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

Aloe0417 --多表查询04

2024-04-17 15:06:06
0
129


-- 3.满外连接
# MYSQL不支持 FULL 来进行满外连接
-- UNION:会执行去重操作
-- UNION ALL :不会执行去重操作
-- 在实际的开发中,能用UNION ALL 不用 UNION

-- 内连接
#SQL99
SELECT employee_id,department_name 
FROM employees e JOIN departments d
ON e.department_id = d.department_id;
#SQL92
SELECT e.employee_id,d.department_name
FROM employees e,departments d
WHERE e.department_id = d.department_id;

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

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

-- 左表无内连接
SELECT e.employee_id,d.department_name
FROM employees e LEFT JOIN departments d
ON e.department_id = d.department_id
WHERE d.department_id IS NULL;

-- 右表无内连接
SELECT e.employee_id,d.department_name
FROM employees e RIGHT JOIN departments d
ON e.department_id = d.department_id
WHERE e.department_id IS NULL;

-- 满外连接
#1.左外连接 UNION ALL 右表无内连接(即是两个查询用
#UNION ALL连接)
SELECT e.employee_id,d.department_name
FROM employees e LEFT JOIN departments d
ON e.department_id = d.department_id
UNION ALL
SELECT e.employee_id,d.department_name
FROM employees e RIGHT JOIN departments d
ON e.department_id = d.department_id
WHERE e.department_id IS NULL;
#2.右外连接 UNION ALL 左表无内连接(即是两个查询用
#UNION ALL连接)
SELECT e.employee_id,d.department_name
FROM employees e RIGHT JOIN departments d
ON e.department_id = d.department_id
UNION ALL 
SELECT e.employee_id,d.department_name
FROM employees e LEFT JOIN departments d
ON e.department_id = d.department_id
WHERE d.department_id IS NULL;

-- 左表无内连接 UNION ALL 右表无内连接
SELECT e.employee_id,d.department_name
FROM employees e LEFT JOIN departments d
ON e.department_id = d.department_id
WHERE d.department_id IS NULL
UNION ALL
SELECT e.employee_id,d.department_name
FROM employees e RIGHT JOIN departments d
ON e.department_id = d.department_id
WHERE e.department_id IS NULL;
评论
意见反馈