高级查询

高级查询

1、连接查询

select * from info,nation——形成笛卡尔积    会形成一个一一对应

select * from info,nation where info.nation=nation.code

select info.code,info.name,sex,nation.name,birthday from info,nation where info.nation=nation.code

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、子查询:一条SQL语句中包含两个查询,其中一个是父查询(外层查询),另一个是子查询(里层查询),子查询的结果作为父查询条件

——查询民族为汉族的所有人员信息

select *from info where nation=(select code from nation where name='汉族')

(1)无关子查询

子查询可以单独执行,子查询和父查询没有一定的关系

——查询系列是宝马5系的所有汽车信息

select * from car where brand=(select brand _code from brand where brand_name='宝马5系')

(2)相关子查询

查找油耗低于该系列平均油耗的汽车

select * from car where oil<(该系列的平均油耗)

select avg(oil) from car where brand=(该系列)

select * from car a where oil<(select avg(oil) from car b where b. brand=a.brand)

原文地址:https://www.cnblogs.com/yp11/p/5726237.html