mysql七種常用的JOIN查詢
阿新 • • 發佈:2017-10-18
left join 去除 實現 -1 替代 images 查詢 重復數據 sele 1 A、B兩表共有
select * from tbl_emp a inner join tbl_dept b on a.deptId = b.id;
2 A、B兩表共有+A的獨有
select * from tbl_emp a left join tbl_dept b on a.deptId = b.id;
3 A、B兩表共有+B的獨有
select * from tbl_emp a right join tbl_dept b on a.deptId = b.id;
4 A的獨有
select * from tbl_emp a left join tbl_dept b on a.deptId = b.id where b.id is null;
5 B的獨有
select * from tbl_emp a right join tbl_dept b on a.deptId = b.id where a.deptId is null; #B的獨有
6 AB全有
#MySQL Full Join的實現 因為MySQL不支持FULL JOIN,下面是替代方法
#left join + union(可去除重復數據)+ right join
SELECT * FROM tbl_emp A LEFT JOIN tbl_dept B ON A.deptId = B.id
UNION
SELECT * FROM tbl_emp A RIGHT JOIN tbl_dept B ON A.deptId = B.id
7 A的獨有+B的獨有
SELECT * FROM tbl_emp A LEFT JOIN tbl_dept B ON A.deptId = B.id WHERE B.`id` IS NULL
UNION
SELECT * FROM tbl_emp A RIGHT JOIN tbl_dept B ON A.deptId = B.id WHERE A.`deptId` IS NULL;
mysql七種常用的JOIN查詢