高级查询几种方法

1.连接查询 1)逗号 ,where select * from Info,nation #笛卡尔积 表与表之间链接在from后面,逗号分开 selsct *(info.code,info.name.sex,nation.name,info.birthday) from Info,nation where Info .nation=nation.cod筛选出自己的项目 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联合查询注意code name 列的结构,列名必须一样 3.子查询 1)无关子查询

外层查询(里层查询) 子查询的结果当做父查询的条件

子查询:select code from nation where name=‘汉族’ 父查询:select * from info where nation=‘’ 结合;select * from info where nation =(select code from nation where name=‘汉族’)

2)相关子查询 查询汽车表中油耗低于该系列平均油耗的所有汽车信息 父查询:select * from car where oil<(该系列平均油耗) 子查询:select avg(oil)from car where brand=‘某个系列’ select * from car a where oil<(select svg(oil)from car b where b.brand=a.brand) 添加虚拟表名,只用在相关子查询中

原文地址:https://www.cnblogs.com/naqiang/p/5531224.html