首页 小组 文章 相册 留言本 用户 搜索 我的社区 在线学堂 商城 购物车 支付钱包

NNN

2024-04-19 17:05:15
1
147


#满外连接(mysql不支持使用FULL OUTER JOIN)
SELECT employees_id,department_name
FROM employees e FULL OUTER JOIN departments d
ON e.department_id = d.department_id;

#开发中能用union all 就不用union(需要去重 效率低)

#左右各一半的话 左中+右中

#中图
#99
SELECT employee_id,department_name
FROM employees e JOIN departments d
ON e.department_id=d.department_id;

#92
SELECT employee_id,department_name
FROM employees e ,departments d
WHERE e.department_id=d.department_id;

#左上图:左外连接
SELECT employee_id,department_name
FROM employees e LEFT JOIN departments d
ON e.department_id= d.department_id;

#右上图:右外连接
SELECT employee_id,department_name
FROM employees e RIGHT JOIN departments d
ON e.department_id=d.department_id;

#左中图
SELECT employee_id,department_name
FROM employees e LEFT JOIN departments d
ON e.department_id= d.department_id
WHERE e.department_id IS NULL;

#右中图
SELECT employee_id,department_name
FROM employees e RIGHT JOIN departments d
ON e.department_id=d.department_id
WHERE e.department_id IS NULL;

#满外连接 
#左上+右中
SELECT employee_id,department_name
FROM employees e LEFT JOIN departments d
ON e.department_id= d.department_id
UNION ALL 
SELECT employee_id,department_name
FROM employees e RIGHT JOIN departments d
ON e.department_id=d.department_id
WHERE e.department_id IS NULL;

#右上+左中
SELECT employee_id,department_name
FROM employees e RIGHT JOIN departments d
ON e.department_id=d.department_id
UNION ALL
SELECT employee_id,department_name
FROM employees e LEFT JOIN departments d
ON e.department_id= d.department_id
WHERE e.department_id IS NULL;

#左右各一半:左中+右中
SELECT employee_id,department_name
FROM employees e LEFT JOIN departments d
ON e.department_id= d.department_id
WHERE e.department_id IS NULL
UNION ALL
SELECT employee_id,department_name
FROM employees e RIGHT JOIN departments d
ON e.department_id=d.department_id
WHERE e.department_id IS NULL;
评论 (1)
意见反馈