Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 在两个时间段相交处连接_Mysql_Cross Join - Fatal编程技术网

Mysql 在两个时间段相交处连接

Mysql 在两个时间段相交处连接,mysql,cross-join,Mysql,Cross Join,我们有两个表,正试图找出如何在两个时间段相交的地方进行交叉连接 第一个表(employment)包含4列: EmployerID, UserID, StartDate, EndDate UserID, Status, StartDate, EndDate 第二个表(status\u history)也包含4列: EmployerID, UserID, StartDate, EndDate UserID, Status, StartDate, EndDate employment表包含显示

我们有两个表,正试图找出如何在两个时间段相交的地方进行交叉连接

第一个表(
employment
)包含4列:

EmployerID, UserID, StartDate, EndDate
UserID, Status, StartDate, EndDate
第二个表(
status\u history
)也包含4列:

EmployerID, UserID, StartDate, EndDate
UserID, Status, StartDate, EndDate
employment
表包含显示每个用户与哪个“职务”关联的记录,以及关联的时间(开始日期和结束日期)。类似地,
status\u history
包含显示用户是否处于活动/非活动状态(就业或失业)的记录,还包含StartDate和EndDate

我们试图构建一个视图,在两个表之间创建一个适当的“交叉连接”

EmployerID, UserID, Status, StartDate, EndDate
我尝试创建一个SQL小提琴,但由于某种原因,我从他们那里得到了一个错误。因此,我提供了以下模式:

CREATE TABLE employment
    (`EmployerID` int, `UserID` int, `StartDate` date, `EndDate` date);

CREATE TABLE status_history
    (`UserID` int, `Status` varchar(10), `StartDate` date, `EndDate` date);

INSERT INTO employment
    (`EmployerID`, `UserID`, `StartDate`, `EndDate`)
VALUES
    (123, 111, '2017-01-01', '2017-03-04'),
    (345, 111, '2017-03-04', '2017-03-07'),
    (567, 111, '2017-03-07', '2017-04-10'),
    (789, 111, '2017-04-10', NULL)
;

INSERT INTO status_history
    (`UserID`, `Status`, `StartDate`, `EndDate`)
VALUES
    (111, 'Active', '2017-01-01', '2017-02-17'),
    (111, 'Inactive', '2017-02-17', '2017-03-02'),
    (111, 'Active', '2017-03-02', '2017-03-09'),
    (111, 'Inactive', '2017-03-09', NULL),
;
根据数据,我希望检索以下行:

+------------+---------+-----------+-------------+-------------+
| EmployerID |  UserID |   Status  |  StartDate  |   EndDate   |
+------------+---------+-----------+-------------+-------------+
|        123 |     111 |  Active   |  2017-01-01 |  2017-02-17 |
|        123 |     111 |  Inactive |  2017-02-17 |  2017-03-02 |
|        123 |     111 |  Active   |  2017-03-02 |  2017-03-04 |
|        345 |     111 |  Active   |  2017-03-04 |  2017-03-07 |
|        567 |     111 |  Active   |  2017-03-07 |  2017-03-09 |
|        567 |     111 |  Inactive |  2017-03-09 |  2017-04-10 |
|        789 |     111 |  Inactive |  2017-04-10 |  NULL       |
+------------+---------+-----------+-------------+-------------+

任何帮助都将不胜感激

我反复思考这个问题,终于找到了答案

答案很简单:我在
UserID
的时间段相交处进行连接,得到了结果(即
StartDate
from
employment
EndDate
from
status\u history
EndDate
from
employment
StartDate
from
status\u history
。对于NULL EndDate的边缘情况,我使用今天的日期


然后,我只需选择两个起始日期中最大的一个,两个结束日期中最小的一个。

使用内部联接。显示您尝试了什么?我们尝试了一些事情,但没有一个给出正确的结果。最新的查询是一个简单的内部联接,从sh.UserID=e.UserID和sh.StartDate=e上的
状态历史
就业
.StartDate和sh.EndDate=e.EndDate
哪一个是唯一的
EmployerID
UserID