Join图图
http://t.csdn.cn/sutox
MySQL的七种join图解
一、内连接
二、左连接
三、右连接
四、左外连接
五、右外连接
六、全连接
七、全外连接
一、内连接
描述:连个表共有的记录
select * from TableA A inner join TableB B on A.Key =B.key;
1
二、左连接
描述:求A表所有记录以及AB表共有的记录(B表没有的字段补null)
select * from tb1_emp a left join tb1_dept b on a.deptId = b.id;
三、右连接
描述: 求B表所有记录以及AB表共有的记录(A表没有的字段补null)
select * from tb1_emp a right join tb1_dept b on a.deptId = b.id;
四、左外连接
描述:从AB表中求A表独有的记录
select * from tb1_emp a left join tb1_dept b on a.deptId = b.id where b.id is null;
五、右外连接
描述:从AB表中求B表中独有的记录
select * from tb1_emp a right join tb1_dept b on a.deptId = b.id where a.deptId is null;
六、全连接
描述:查询AB表中所有的记录
select * from tb1_emp a left join tb1_dept b on a.deptId = b.id
union
select * from tb1_emp a right join tb1_dept b on a.deptId = b.id
Union合并+去重
七、全外连接
描述:查询A表独有的记录和B表独有的记录
select * from tb1_emp a left join tb1_dept b on a.deptId = b.id where b.id is null
union
select * from tb1_emp a right join tb1_dept b on a.deptId = b.id where a.deptId is null;