高级查询

一、联合查询(行的扩展)

select code ,name from info
union
select code, name from 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 info.code,info.name,sex,nation.name,birthday from info join nation on info.nation=nation.code # left jion...on...左联以左边为主,  right jion...on右连

显示:

三、子查询(子查询的结果作为父查询的条件使用)

无关子查询(子查询和父查询没关系,子查询可以单独拿出来使用)

select查询可以无限嵌套使用

查找民族为“汉族”的所有人员信息

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

相关子查询

查询油耗低于该系列平均油耗的汽车信息

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

 a   b   是虚拟的

显示:

原文地址:https://www.cnblogs.com/xiaohaihuaihuai/p/8193682.html