sql - 获取所有从未休假过的员工的姓名
问题描述
员工和 DaysTaken 表
嗨,我面临一个问题,即员工“John Smith”请“病假”和“假期”。这是我目前的答案
SELECT DISTINCT CONCAT(FirstName, ' ', MiddleName, ' ', LastName) AS 'CompleteName' from Employee
RIGHT JOIN DaysTaken ON
Employee.EmployeeID =
DaysTaken.EmployeeID
WHERE (DaysTaken.vacationtype = 'sick day')
解决方案
右外连接很难阅读,不建议使用它们。在您的查询中,您选择所有病假并外部加入相关员工。由于可能所有的病假都由员工使用,这与单纯的内部连接相同。
因此,您选择所有员工的病假天数。然后与DISTINCT
您一起向每位生病的员工展示。这与您的请求标题所要求的完全不同。(并且加入所有这些行只是为了消除它们中的大多数,DISTINCT
无论如何我都不会遵循这种方法。)
您的请求标题说
获取所有从未休假过的员工的姓名
为了获取员工姓名,您将从员工表中进行选择。您只需要满足条件的员工。条件最适合该WHERE
子句。条件是该员工不存在休假条目。[NOT] EXISTS
我们用or检查表中是否存在一行[NOT] IN
。
不存在
select concat_ws(' ', firstname, middlename, lastname) as complete_name
from employee e
where not exists
(
select null
from daystaken dt
where dt.vacationtype = 'vacation'
and dt.employeeid = e.employeeid
);
没有在
select concat_ws(' ', firstname, middlename, lastname) as complete_name
from employee
where employeeid not in
(
select employeeid
from daystaken
where vacationtype = 'vacation'
);
在图像中只有vacationtype
“假期”或“病假”的行。如果有其他条件,where vacationtype = 'vacation'
可能仍然合适,但您可能还需要另一个条件,例如where vacationtype <> 'sick day'
。
推荐阅读
- c - C: int 或 unsigned int 我用于指针增加的类型
- c++ - 如何在循环中创建具有自身实例指针的对象?
- bluetooth - 连接第二个 BLE 设备后 BLE 响应时间变慢
- javascript - 带有 material-ui 的 React-hook-form 不会保持 onBlur() 方法中的值变化
- r - 如何正确地将 df 转换为时间序列并在同一管道中清理它?
- python - 当我将 PCL(点云库)与 PythonLibs 一起使用时,为什么我的 CMakeLists.txt 不起作用?
- php - 使mysql查询动态化
- python - 在 django_filters 中搜索部分匹配和不区分大小写
- node.js - 如何摆脱:警告:找不到任何匹配模式“dotenv_config_path=.env.test”的文件
- sqlite - (SQLITE) 如何使用子查询将另一个表的总和相加