总结SQL语句left join、right join、inner join以及where查询:
- left join 左连接:以左侧的表为主,查询结果与左侧表的记录数相同
- right join 右连接:以右侧表为主,查询结果与右侧表的记录数相同
例如:
表a:
aid adata
1 a1
2 a2
3 a3
表b:
bid bdata
1 b1
2 b2
3 b3
左链接:
select * from a left join b on a.aid=b.bid;
结果:
aid adata bid bdata
1 a1 1 b1
2 a2 2 b2
3 a3
右连接:
select * from a right join b on a.aid=b.bid;
结果:
aid adata bid bdata
1 a1 1 b1
2 a2 2 b2
4 b4
- inner join内连接:查找两个表相匹配的记录,等同于where
内连接
select * from a inner join b on a.aid=b.bid;
结果:
aid adata bid bdata
1 a1 1 b1
2 a2 2 b2
等同于:select * from a,b where a.aid=b.bid
注意:没有left inner join,语法错误!
left join 是 left outer join 的简单写法;right join 是 right outer join 的简单写法