2

我是一个完整的 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

如果他/她忘记了 Time Out 或 time in 它显示 MySQL 返回一个空的结果集(即零行)。(查询耗时 0.0016 秒)

我想要的是

Employee Number | date_created |Time_in   | Time_Out
         3      | 2013-07-15   | 11:50:00 | Null

请帮我解决一下这个。我非常需要它,谢谢。

4

1 回答 1

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

样本输出:

+-------+-------------+----------+----------+
| 雇员 | 日期创建 | 时间_in | 超时 |
+-------+-------------+----------+----------+
| 2 | 2013-07-15 | 11:08:07 | 15:00:00 |
| 3 | 2013-07-15 | 11:50:00 | 空 |
| 4 | 2013-07-15 | 空 | 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演示

于 2013-07-15T10:12:31.410 回答