高级查询

高级查询
1.链接查询 对列的扩展
(1)逗号  ,  where
select * from info,nation  形成笛卡尔积  表与表链接在from后面,逗号分开
select * from info,nation where info.nation=nation.code  后面添加条件
select info.code,info.name,info.sex,info.name,info.birthday from info,nation where info.nation=nation.code     筛选出自己要的项目
(2)关键字join   on
select * from info join nation
select * from info join nation on info.nation=nation.code  join和on是一对
2.联合查询 对行的扩展
select code,name from info
union
select code,name from nation   联合查询注意列的结构,列名必须相同
3.子查询:
(1)无关子查询
外层查询(里层查询)
子查询的结果,当作复父查询的条件
例子:查找汉族所有人
子查询:select code from nation where name='汉族'
父查询:select*from info nation=''
结合:select*from info nation=(select code from nation where name='汉族')
(2)相关子查询
例子:查询汽车表中油耗低于该系列平均油耗的所有车辆信息
父查询: select * from car where dil<(该系平均油耗)
子查询: select avg(oil) from car where brand='某个系列'
select * from car a where a.oil<(select svg(oil) from car b where b.brand=a.brand) 添加虚拟表名,只用在相关子查询中

原文地址:https://www.cnblogs.com/panyiquan/p/5540236.html