常见的join查询
sql执行顺序
手写:
select * from 表 where 条件 group by 字段 having 条件 order by 字段
机读:
from 表1
on 连接条件 连接类型 join 表2
where 条件
group by 字段
having 条件
select distinct 查询列
order by 条件
limit
补充:having的用法,having后面用于筛选符合条件的分组数据,也可用于聚合函数。
总结
7种sql编写
最后两种
全有:
select * from A left join B on A.id = B.aid
union
select * from A right join B on A.id = B.aid
补集:
select * from A left join B on A.id = B.aid where B.id is null
union
select * from A right join B on A.id = B.aid where A.id is null