数据库
首页 > 数据库> > MySQL(21)——sql99语法(左(右)外连接)

MySQL(21)——sql99语法(左(右)外连接)

作者:互联网

1. 概念

2. 示例

(1)查询男朋友不在男生表的女生名----------左外连接

SELECT 
    b.name
FROM
    beauty b
LEFT 
OUTER JOIN
    boys bo
ON
    b.`boyfriend_id`=bo.`id`
WHERE
    bo.`id` IS NULL;

(2)查询男朋友不在男生表的女生名----------右外连接

SELECT 
    b.name
FROM
    boys bo
RIGHT 
OUTER JOIN
    beauty b
ON
    b.`boyfriend_id`=bo.`id`
WHERE
    bo.`id` IS NULL;

(3)查询哪个部门没有员工-----------------左外连接

SELECT
	d.*,
	e.employee_id
FROM
	`departments` d
LEFT OUTER JOIN
	`employees` e
ON
	d.`department_id`=e.`department_id`
WHERE
	e.`department_id` IS NULL;

(4)查询哪个部门没有员工-----------------右外连接

SELECT
	d.*,
	e.employee_id
FROM
    `employees` e
RIGHT OUTER JOIN
	`departments` d
ON
	d.`department_id`=e.`department_id`
WHERE
	e.`department_id` IS NULL;

标签:OUTER,21,bo,右外,连接,sql99,MySQL,department,id
来源: https://blog.csdn.net/www_dong/article/details/120355239