Mysql 最后一次进门,最后一次出门

Mysql 最后一次进门,最后一次出门,mysql,Mysql,情况就是这样。我有一个MYSQL考勤表,记录如下: Id DateTime Door Employee_id 1 2016-01-01 08:00:00 In 100 2 2016-01-01 09:00:00 Out 100 3 2016-01-01 09:11:00 Enter 100 4 2016-01-01 09:12:00 Exit 100 5 2016-01-

情况就是这样。我有一个MYSQL考勤表,记录如下:

   Id   DateTime             Door   Employee_id
    1   2016-01-01 08:00:00  In     100
    2   2016-01-01 09:00:00  Out    100
    3   2016-01-01 09:11:00  Enter  100
    4   2016-01-01 09:12:00  Exit   100
    5   2016-01-01 09:15:00  In     100
    6   2016-01-01 09:30:00  In     100
    7   2016-01-01 10:00:00  Out    100
    8   2016-01-01 11:00:00  In     100
    9   2016-01-01 12:00:00  In     100
   10   2016-01-01 13:00:00  In     100
   11   2016-01-01 13:30:00  Out    100
   10   2016-01-01 14:00:00  Out    100
   12   2016-01-01 15:00:00  In     100
我希望输出为“输入”和“输出”门的最后一次时钟输入和最后一次时钟输出,如下所示。如果在最后一次时钟输入后没有时钟输出,只需忽略时钟输入。如果除了“进”和“出”之外还有其他门,也请忽略

Id   Clock In             Clock Out             Employee Id
 1   2016-01-01 08:00:00  2016-01-01 09:00:00   100
 2   2016-01-01 09:30:00  2016-01-01 10:00:00   100
 3   2016-01-01 13:00:00  2016-01-01 14:00:00   100

我已经在这件事上纠缠了好几天了。请帮帮我,伙计们。提前感谢您的帮助。

以下是使用您的数据进行演示的解决方案

SQL:

输出:

mysql> SELECT * FROM attendance;
:+------+---------------------+-------+-------------+
| Id   | DateTime            | Door  | Employee_id |
+------+---------------------+-------+-------------+
|    1 | 2016-01-01 08:00:00 | In    |         100 |
|    2 | 2016-01-01 09:00:00 | Out   |         100 |
|    3 | 2016-01-01 09:11:00 | Enter |         100 |
|    4 | 2016-01-01 09:12:00 | Exit  |         100 |
|    5 | 2016-01-01 09:15:00 | In    |         100 |
|    6 | 2016-01-01 09:30:00 | In    |         100 |
|    7 | 2016-01-01 10:00:00 | Out   |         100 |
|    8 | 2016-01-01 11:00:00 | In    |         100 |
|    9 | 2016-01-01 12:00:00 | In    |         100 |
|   10 | 2016-01-01 13:00:00 | In    |         100 |
|   11 | 2016-01-01 13:30:00 | Out   |         100 |
|   10 | 2016-01-01 14:00:00 | Out   |         100 |
|   12 | 2016-01-01 15:00:00 | In    |         100 |
+------+---------------------+-------+-------------+
13 rows in set (0.00 sec)

mysql>
mysql> -- SQL needed:
mysql> SELECT  @id:=@id+1 Id,
    ->         MAX(IF(Door = 'In', DateTime, NULL)) `Check In`,
    ->         MAX(IF(Door = 'Out', DateTime, NULL)) `Check Out`,
    ->         Employee_id
    -> FROM (SELECT *,
    ->             CASE WHEN (Door != 'Out' AND @last_door = 'Out')
    ->                  THEN @group_num:=@group_num+1
    ->                  ELSE @group_num END door_group,
    ->             @last_door:=Door
    ->         FROM attendance JOIN (SELECT @group_num:=1, @last_door := NULL) a
    ->       ) t JOIN (SELECT @id:=0 ) b
    -> GROUP BY t.door_group
    -> HAVING SUM(Door = 'In') > 0 AND SUM(Door = 'Out') > 0;
+------+---------------------+---------------------+-------------+
| Id   | Check In            | Check Out           | Employee_id |
+------+---------------------+---------------------+-------------+
|    1 | 2016-01-01 08:00:00 | 2016-01-01 09:00:00 |         100 |
|    2 | 2016-01-01 09:30:00 | 2016-01-01 10:00:00 |         100 |
|    3 | 2016-01-01 13:00:00 | 2016-01-01 14:00:00 |         100 |
+------+---------------------+---------------------+-------------+
3 rows in set (0.00 sec)
试试这个代码

Select @x := -1;
Select @pDate := NOW();

SELECT all2.link, all2.prevD as 'Clock In', all2.DateTime as 'Clock Out'
FROM
 (SELECT
    DateTime, id, Door, @x:=@x AS prev,
    if(@x > 0, @x, -1) as link,
    @x:=IF(door = 'In', id, if(door = 'Out', id, -1)) AS cure,
    @pDate as prevD,
    @pDate:=IF(door = 'In', DateTime, NULL) AS pDate,
    @x:=IF(door = 'Out', -1, @x) as reseter
FROM
    test.doors) as all2
WHERE all2.link != -1 AND all2.Door = 'Out'
事实上,有一个bug是: “我无法识别最后一个
Out
状态,因此我希望您尝试解决它,因为我没有足够的时间。”


最美好的祝愿

我不理解id栏。Shurly shome mishtakeid列只是一个主键,在插入新记录时自动递增。不在上面的示例中。感谢代码Dylan,它在使用此表示例时有效。但当我将其更改为不同的列名和不同的示例数据时,它似乎不起作用。如果我错了,请纠正我。这是我使用的样本。“In”和“Out”在哪里?我用另一个分别代表它们的varchar替换了“In”和“Out”。哦,现在我知道为什么了。我刚才搜索了错误的日期范围。谢谢你的回答。将在我的本地mysql上试用。:)在我的本地mysql中进行了测试,但我想知道为什么我需要运行查询两次才能获得所有的行?如果我第一次运行查询,它只返回一行。你曾经遇到过这样的问题吗?
Select @x := -1;
Select @pDate := NOW();

SELECT all2.link, all2.prevD as 'Clock In', all2.DateTime as 'Clock Out'
FROM
 (SELECT
    DateTime, id, Door, @x:=@x AS prev,
    if(@x > 0, @x, -1) as link,
    @x:=IF(door = 'In', id, if(door = 'Out', id, -1)) AS cure,
    @pDate as prevD,
    @pDate:=IF(door = 'In', DateTime, NULL) AS pDate,
    @x:=IF(door = 'Out', -1, @x) as reseter
FROM
    test.doors) as all2
WHERE all2.link != -1 AND all2.Door = 'Out'