简体   繁体   English

如何在内部联接中放入空集

[英]How to put empty set in inner join

I am a complete beginner in MySql and I wanted you to help me. 我是MySql的一个完整的初学者,我希望您能帮助我。 I want a DTR User Interface where there is time in and time out. 我想要一个DTR User Interface ,那里有超时和超时。 So I have a fields like - id_biometrics,empno,datecreated,time_created,status,device the problem is if the employee forgot to time in or time out, I want to put the time in or time out to "NULL" here is what I coded. 所以我有一个字段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

And what my output here if he completes his time in and time out for the day is. 如果他完成一天中的时间和超时,我在这里的输出是什么。

Employee Number|date_created |Time_in   | Time_Out
             2 | 2013-07-15  | 11:08:07 | 15:00:00

And if he/she forgots to Time Out or time in it shows MySQL returned an empty result set (ie zero rows). 并且如果他/她忘记超时或超时,则表明MySQL返回了空结果集(即零行)。 ( Query took 0.0016 sec ) (查询花费了0.0016秒)

What I wanted is 我想要的是

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

Please help me with this. 请帮我解决一下这个。 I needed it badly thanks. 我非常需要它,谢谢。

Are you looking for this? 您在找这个吗?

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

Sample output: 样本输出:

+-------+--------------+----------+----------+
| 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 |
+-------+--------------+----------+----------+

Here is SQLFiddle demo 这是SQLFiddle演示

UPDATE If you may have multiple in or out records and you always want to fetch first in and last out then just use MAX() aggregate for time_out UPDATE如果您可能有多个输入或输出记录,并且您始终希望先取后取,则只需将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

Here is SQLFiddle demo 这是SQLFiddle演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM