LeetCode数据库---1965. 丢失信息的雇员

题目:

表: Employees

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| employee_id | int     |
| name        | varchar |
+-------------+---------+
employee_id 是这个表的主键。
每一行表示雇员的id 和他的姓名。
表: Salaries

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| employee_id | int     |
| salary      | int     |
+-------------+---------+
employee_id is 这个表的主键。
每一行表示雇员的id 和他的薪水。
 

写出一个查询语句,找到所有 丢失信息 的雇员id。当满足下面一个条件时,就被认为是雇员的信息丢失:

雇员的 姓名 丢失了,或者
雇员的 薪水信息 丢失了,或者
返回这些雇员的id  employee_id , 从小到大排序 。

查询结果格式如下面的例子所示。


示例 1:

输入:
Employees table:
+-------------+----------+
| employee_id | name     |
+-------------+----------+
| 2           | Crew     |
| 4           | Haven    |
| 5           | Kristian |
+-------------+----------+
Salaries table:
+-------------+--------+
| employee_id | salary |
+-------------+--------+
| 5           | 76071  |
| 1           | 22517  |
| 4           | 63539  |
+-------------+--------+
输出:
+-------------+
| employee_id |
+-------------+
| 1           |
| 2           |
+-------------+
解释:
雇员1,245 都工作在这个公司。
1号雇员的姓名丢失了。
2号雇员的薪水信息丢失了。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/employees-with-missing-information

知识点:

1. MySQL目前不支持Full Join(全连接)。

    理论上,全连接是左连接和右连接的组合。

    完整的外部连接包括联接表中的所有行,无论另一个表是否具有匹配的行。

    所以实现方法可以是:左连接 + UNION(可去除重复数据) + 右连接

2. UNION和UNION ALL区别

    UNION:去重,删掉重复的记录

    UNION ALL:会把所有的记录返回,且效率高于UNION

# 解题思路:先左连接查出salary为null的id,
# 再右连接,查出name为null的id,
# 然后用union连接
select e.employee_id
from employees as e left outer join salaries as s
on e.employee_id = s.employee_id
where s.salary is null
union
select s.employee_id
from employees as e right outer join salaries as s
on e.employee_id = s.employee_id
where e.name is null
order by employee_id;

 

posted @ 2022-05-15 21:41  射手座的小怪兽  阅读(100)  评论(0编辑  收藏  举报