我有 3 个表。第一个是人员名单:
表人员
Id_personnel Name
1 John
2 Alice
3 Tom
4 Charles
5 Ben
第二张是带病假的表:
表医疗
Id_personnel Start_date End_date
3 2012-08-02 2012-08-05
2 2012-08-02 2012-08-15
4 2012-10-04 2012-10-06
2 2012-10-02 2012-10-15
2 2012-09-20 2012-09-21
第三张是放假的表:
表假期
Id_personnel Start_date End_date
3 2012-08-02 2012-08-05
2 2012-02-02 2012-02-15
5 2012-10-01 2012-10-05
我有两个事件日期:Symposium_start_date
和一个 Symposium_end_date
(作为变量),我想选择所有 Personnel.Id_personnel
和 Personnel.Name
可参加座谈会(非病假或节假日)
在我的例子中,如果 Symposium_start_date = 2012-10-02
和 Symposium_end_date = 2012-10-06
结果必须是:
Id_personnel Name
1 John
3 Tom
(如您所见,爱丽丝和查尔斯休病假,本休假)
谢谢!
最佳答案
对于 MS SQL 服务器。
declare @start date = '2012-10-02', @end date = '2012-10-10'
select personnel.*
from personnel
left join holiday on personnel.Id_personnel = holiday.Id_personnel
and holiday.start_date<=@end
and holiday.end_date>=@start
left join medical on personnel.Id_personnel= medical.Id_personnel
and medical.start_date<=@end
and medical.end_date>=@start
where holiday.Id_personnel is null
and medical.Id_personnel is null
关于特定时间段内可用人员的sql SELECT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12778130/