-- 合并两年销售数据
SELECT customer_id, product, amount FROM sales_2023
UNION ALL
SELECT customer_id, product, amount FROM sales_2024
ORDER BY customer_id;
结果说明:
customer_id
product
amount
1
Phone
5000
2
Laptop
8000
2
Laptop
8000
3
Tablet
3000
3
Tablet
3000
4
Watch
2000
提示:
UNION ALL
UNION ALL
不去重,性能优于
UNION
UNION
。如果确定数据无重复或不需要去重,优先使用
UNION ALL
UNION ALL
。
去重合并
使用
UNION
UNION
合并结果集并自动去除重复行。
-- 合并两年销售数据并去重
SELECT customer_id, product, amount FROM sales_2023
UNION
SELECT customer_id, product, amount FROM sales_2024
ORDER BY customer_id;
结果说明:
customer_id
product
amount
1
Phone
5000
2
Laptop
8000
3
Tablet
3000
4
Watch
2000
查找共同数据
使用
INTERSECT
INTERSECT
返回两个结果集中都存在的记录(交集)。
-- 查找两年都有购买的客户和产品
SELECT customer_id, product, amount FROM sales_2023
INTERSECT
SELECT customer_id, product, amount FROM sales_2024
ORDER BY customer_id;
结果说明:
customer_id
product
amount
2
Laptop
8000
3
Tablet
3000
查找差异数据
使用
EXCEPT
EXCEPT
返回在第一个结果集中存在但在第二个结果集中不存在的记录(差集)。
-- 查找 2023 年有但 2024 年没有的客户(流失客户)
SELECT customer_id, product FROM sales_2023
EXCEPT
SELECT customer_id, product FROM sales_2024
ORDER BY customer_id;
结果说明:
customer_id
product
1
Phone
-- 查找 2024 年新增的客户
SELECT customer_id, product FROM sales_2024
EXCEPT
SELECT customer_id, product FROM sales_2023
ORDER BY customer_id;
结果说明:
customer_id
product
4
Watch
多表对比
组合使用集合操作进行更复杂的数据对比。
-- 查找仅在某一年出现的客户(对称差集)
(SELECT customer_id FROM sales_2023
EXCEPT
SELECT customer_id FROM sales_2024)
UNION ALL
(SELECT customer_id FROM sales_2024
EXCEPT
SELECT customer_id FROM sales_2023)
ORDER BY customer_id;
结果说明:
customer_id
1
4
清理测试数据
完成集合操作验证后,建议清理测试表:
-- 删除测试表
DROP TABLE IF EXISTS sales_2023;
DROP TABLE IF EXISTS sales_2024;