如何创建基于datetime排除的sql视图

时间:2013-05-31 13:04:36

标签: mysql sql

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

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;

我想要与此相反,那些没有参赛作品的员工?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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)

this question

非常相似