题目来源:
leetcode题目,网址:1965. 丢失信息的雇员 - 力扣(LeetCode)
解题思路:
首先使用 union 找出所有雇员的编号,然后获得既有工资又有姓名的雇员编号的表 a,接着找出不在表 a 中的雇员编号,最后按要求输出。
解题代码:
# Write your MySQL query statement below
select a.employee_id
from (
select employee_id from Employees
union
select employee_id from Salaries
) as a
where employee_id not in (
select a.employee_id
from Employees as a inner join Salaries as b on a.employee_id=b.employee_id
)
order by a.employee_id
总结:
写得有点蠢,应该有更好的写法。
无官方题解。题解区有种写法是先使用 union all 获得所有雇员及相关信息,然后分组聚合后输出数据条数只有一条的雇员。
union 和 union all 都可以合并结果集,但union 会自动去除结果集中的交集,而 union all 不会。