我是MySql的初学者,我希望你能帮助我。我想要DTR User Interface
,其中有时间和时间。所以我有一个字段 - id_biometrics,empno,datecreated,time_created,status,device
问题是,如果员工忘记进入或超时,我想把时间或时间用于“NULL”这里是我编码的。
SELECT start_log.empno AS "Employee Number", start_log.date_created, start_log.time_created AS "Time In", end_log.time_created AS "Time Out"
FROM biometrics AS start_log
INNER JOIN biometrics AS end_log ON start_log.empno = end_log.empno
WHERE start_log.status =0
AND end_log.status =1
AND start_log.empno =2
GROUP BY start_log.date_created, start_log.empno
如果他在这一天完成他的时间和超时,那么我在这里输出的是。
Employee Number|date_created |Time_in | Time_Out
2 | 2013-07-15 | 11:08:07 | 15:00:00
如果他/她忘记了超时或其中的时间,则显示MySQL返回空结果集(即零行)。 (查询花了0.0016秒)
我想要的是
Employee Number | date_created |Time_in | Time_Out
3 | 2013-07-15 | 11:50:00 | Null
请帮我解决这个问题。我非常感谢你。
答案 0 :(得分:1)
你在找这个吗?
SELECT empno, date_created,
MIN(CASE WHEN status = 0 THEN time_created END) time_in,
MIN(CASE WHEN status = 1 THEN time_created END) time_out
FROM biometrics
GROUP BY empno, date_created
示例输出:
+-------+--------------+----------+----------+ | empno | date_created | time_in | time_out | +-------+--------------+----------+----------+ | 2 | 2013-07-15 | 11:08:07 | 15:00:00 | | 3 | 2013-07-15 | 11:50:00 | NULL | | 4 | 2013-07-15 | NULL | 16:00:00 | +-------+--------------+----------+----------+
这是 SQLFiddle 演示
更新如果您可能有多个输入或输出记录,并且您始终要先获取并最后输出,那么只需使用MAX()
聚合time_out
SELECT empno, date_created,
MIN(CASE WHEN status = 0 THEN time_created END) time_in,
MAX(CASE WHEN status = 1 THEN time_created END) time_out
FROM biometrics
GROUP BY empno, date_created
这是 SQLFiddle 演示