企业可以使用三个表来跟踪其员工的缺勤天数。它来自 Joe Celko 的 SQL 谜题集。读者的任务是编写一条 DELETE 语句,以删除累计缺勤 40 天或以上的员工。为此,我们使用子查询。作者提出的一个是这样的:
DELETE FROM Personnel
WHERE emp_id = (SELECT A1.emp_id
FROM Absenteeism AS A1
WHERE A1.emp_id = Personnel.emp_id
GROUP BY A1.emp_id
HAVING SUM(severity_points) >= 40);
我的问题是,为什么要选择相关的?运行像下面这样的东西不是更有效率吗?
DELETE FROM Personnel
WHERE emp_id =
(SELECT emp_id FROM Absenteeism
GROUP BY emp_id
HAVING SUM(severity_points) >= 40)
谢谢!