首页 > 数据库技术 > 详细

MySQL 七种 JOIN

时间:2020-06-14 10:47:05      阅读:49      评论:0      收藏:0      [点我收藏+]
共有
SELECT
    *
FROM
    t_dept a
INNER JOIN t_emp b ON a.id = b.deptId;

左独占+共有

SELECT
    *
FROM
    t_dept a
LEFT JOIN t_emp b ON a.id = b.deptId;

右独占+共有

SELECT
    *
FROM
    t_dept a
RIGHT JOIN t_emp b ON a.id = b.deptId;

左独占

SELECT
    *
FROM
    t_dept a
LEFT JOIN t_emp b ON a.id = b.deptId
WHERE b.deptId IS NULL;

右独占


SELECT
    *
FROM
    t_dept a
RIGHT JOIN t_emp b ON a.id = b.deptId
WHERE a.id IS NULL;

左右全部

SELECT
    *
FROM
    t_dept a
LEFT JOIN t_emp b ON a.id = b.deptId
UNION
SELECT
    *
FROM
    t_dept a
RIGHT JOIN t_emp b ON a.id = b.deptId;

左独占+右独占

SELECT
    *
FROM
    t_dept a
LEFT JOIN t_emp b ON a.id = b.deptId
WHERE b.deptId IS NULL 
UNION
SELECT
    *
FROM
    t_dept a
RIGHT JOIN t_emp b ON a.id = b.deptId
WHERE a.id IS NULL;

MySQL 七种 JOIN

原文:https://blog.51cto.com/13559120/2504351

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!