首页 > 数据库技术 > 详细

T-SQL高级查询语句(父子查询)

时间:2017-03-04 19:02:51      阅读:235      评论:0      收藏:0      [点我收藏+]
 1  T-SQL高级查询语句
 2 
 3 高级查询
 4 
 5 1.连接查询,对结果集列的扩展
 6 select * from info
 7 
 8 select * from info,nation #形成笛卡尔积
 9 select * from info,nation where info.nation=nation.code
10 select info.code,info.name,sex,nation.name,birthday from info,nation where info.nation=nation.code
11 
12 select * from info join nation on info.nation=nation.code
13 
14 2.联合查询,对结果集行的扩展
15 select code,name from info
16 union
17 select code,name from nation

技术分享

 1 3.子查询
 2 
 3 父查询:外层查询
 4 子查询:里层查询
 5 
 6 子查询的结果做为父查询的条件
 7 
 8 (1)无关子查询
 9 子查询在执行的时候和父查询没有关系,子查询可以单独执行
10 
11 1.查询民族为‘汉族’的所有人员信息
12 父查询:select * from info where nation=()
13 子查询:select code from nation where name=‘汉族‘
14 
15 select * from info where nation=(select code from nation where name=‘汉族‘)

技术分享

 1 2.查询系列名为‘宝马5系’的所有汽车信息 2 select * from car where brand=(select brand_code from brand where brand_name=‘宝马5系‘) 

 技术分享

1 (2)相关子查询
2 子查询在执行的时候和父查询有关系,子查询不可以单独执行
3 
4 1.查询汽车表中油耗小于该系列平均油耗的所有汽车信息
5 父查询:select * from car where oil<(该系列平均油耗)
6 子查询:select avg(oil) from car where brand=该系列
7 
8 select * from car as a where oil<(select avg(oil) from car as b where b.brand=a.brand)

技术分享

 

T-SQL高级查询语句(父子查询)

原文:http://www.cnblogs.com/aqxss/p/6207299.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!