时间依赖加入

时间:2017-04-14 11:22:39

标签: mysql sql

我有2个表,我在两个表上都有每个表的开始和结束日期,我需要加入它们,以便记录对两个表上的日期都有效

Table 1
EmpId   StartDate   EndDate
1   1/2/2017    12/31/9999
1   8/10/2016   1/1/2017
1   10/11/2015  8/9/2016
1   2/10/2014   10/19/2015

Table2
JobId EMPID StartDate   EndDate
J1  1        1/2/2017   12/31/9999
J2  1        8/10/2016  1/1/2017
J3  1        2/10/2014  8/9/2016

 Result Set Should be something like this 
EmpId   StartDate   EndDate
1   1/2/2017    12/31/9999   J1
1   8/10/2016   1/1/2017     J2
1   10/11/2015  8/9/2016     J3  
1   2/10/2014   10/19/2015   J4

提前致谢 斯

1 个答案:

答案 0 :(得分:0)

你可以测试这样的查询:

SELECT
    t1.*
    ,t2.JobId
FROM
    table1 t1
LEFT JOIN
    table2 t2
ON 
    t1.EmpId = t2.EmpId 
AND 
    t1.StartDate = t2.StartDate 
AND 
    t1.EndDate = t2.EndDate;
相关问题