0

我需要创建一个视图来生成在特定日期没有条目的员工,我的架构:

employee_master:
employee_id (int)
employee_name (varchar)

employee_time:
employee_id (int)
employee_start_tm (datetime)
employee_end_tm (datetime) 

以下 sql 语句列出了在特定日期确实有条目的所有员工:

select employee_master.employee_name  , employee_time.employee_start_tm 
from  employee_master inner join on employee_master.employee_id  = employee_time.employee_id;

我想要与此相反的,没有条目的员工?

4

2 回答 2

1

如果您希望所有没有任何条目的员工都2013-05-31可以这样做

select m.employee_name
from  employee_master m
inner join employee_time t on t.employee_id  = m.employee_id
group by m.employee_name
having sum('2013-05-31' between employee_start_tm and employee_end_tm) = 0
于 2013-05-31T13:10:57.233 回答
0

使用 NOT EXISTS 或 NOT IN 语句。例如:

SELECT EMPLOYEE_MASTER.EMPLOYEE_NAME FROM EMPLOYEE_MASTER
WHERE NOT EXISTS (SELECT * FROM EMPLOYEE_TIME WHERE EMPLOYEE_TIME.EMPLOYEE_ID = EMPLOYEE_MASTER.EMPLOYEE_ID)

和这个问题很相似

于 2013-05-31T13:15:38.703 回答