这就是场景。我有一个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
我希望输出为“最后一个时钟输入”和“最后一个时钟输出”来自' In'和' Out'门只如下图所示。如果在最后一个时钟之后没有时钟输出,则忽略时钟输入。如果还有其他门,那么' In'和' Out'也忽略了。
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
我已经坚持了很多天了。请帮助我。提前感谢您的帮助。
答案 0 :(得分:1)
以下是使用您的数据进行演示的解决方案。
SQL:
-- data
create table attendance(Id int, DateTime datetime, Door char(20), Employee_id int);
INSERT INTO attendance VALUES
( 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);
SELECT * FROM attendance;
-- SQL needed:
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;
输出:
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)
答案 1 :(得分:0)
试试此代码
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'
实际上有一个错误是:
“我无法识别最后Out
州,所以我希望你能尝试解决它,我没有足够的时间。
祝福