黑马程序员——表连接,子查询

------------------------------------ Windows Phone 7手机开发.Net培训、期待与您交流!------------------------------------

索引
全表扫描:对数据进行检索(select)效率最差的是全表扫描,就是一条条的找。
select *from tableName [where col=...]


创建索引能提高查询效率,但是索引是占空间的,而且添加,更新,删除数据的时候也需要同步更新索引,因此会降低insert,update,delete的速度。我
们一般只在经常检索的字段上(where语句后)创建索引。


创建索引的方式,在表设计器中点击右键,选择“索引/键”——》添加-》在列中专责索引包含的列。


即使创建了索引,任然有可能全表扫描,比如like,函数,类型转换等。


--------------------------------------------------------------------
表连接Join与子查询


join方面看参考书
有客户表(T_Customers)和订单表(T_Orders)两个表,客户表字段为:Id,Name,Age订单字符按为:Id,BilNo,CustomerID,订单表通过
Customerid关
联客户表。


select o.billNo,c.Name,c.age
from t_orders as o jion t_customers as c
on o.customerid=c.id




1,将一个查询语句做为一个结果集供其他sql语句使用,就像使用普通的表一样,被当做结果集的查询语句被称为子查询。所有可以使用表的地方几乎都
可以使用子查询来代替。
如:
select *from (select *from t1 where fage<30) as t2


2。单值做为子查询:select 1as f1,2 as f2,(select min(yearPublished) from t_book) as f3,(select max(yearPublished) from
t_book)) as f4


只有返回一行一列的数据的子查询才能当成单值子查询。下面是错误的
如:
select 1 as f1,2,(select yearPublished from t_book)


select *from t_readerfavorite where fcategoryid=(select fid from
t_category where fname='Story'
)
select *from t_readerfavorite where fcategoryid in (select fid from
t_category)



限制结果集
select *from
(
select row_number() over(ordery by fsalary desc) as rownum,
fnumber,fname,fsalary,fage from t_employee
) as e1
where e1.rownum>=3 and e1.rownum<=5


------------------------------------ Windows Phone 7手机开发.Net培训、期待与您交流!------------------------------------


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值