特定时间段内可用人员的sql SELECT

标签 sql date multiple-tables

我有 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_personnelPersonnel.Name可参加座谈会(非病假或节假日)

在我的例子中,如果 Symposium_start_date = 2012-10-02Symposium_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/

相关文章:

javascript - 使用 momentjs 将日期转换为纪元然后返回日期

PHP MYSQL 查询多个表,其中第二个表返回多行

c++ - 何时使用 sqlite3_blob_write/read?

php - 为每条记录仅获取mysql中的最新数据

python - R/SQL/ python : Extracting connected components from node-edge pairs

sql - Oracle SQL 中查询表值别名

java - SimpleDateFormat java.text.ParseException : Unparseable date

PHP 日期字符串格式

mysql - 从 mySQL 中的各种表中选择列

Scrapy:对数据库的多个返回/返回的建议