我有这样的出勤记录表
+----------------+---------+-----------------------+
| NIP | Nama | Date_Time |
+----------------+---------+-----------------------+
| 050803075201 | Supomo | 2013-02-20 07:45:57 |
| 050803075201 | Supomo | 2013-02-20 17:24:13 |
| 050803075201 | Supomo | 2013-02-21 07:53:40 |
| 050803075201 | Supomo | 2013-02-21 17:31:57 |
| 050803075200 | Teguh | 2013-02-21 20:31:02 |
| 050803075200 | Teguh | 2013-02-20 18:18:07 |
+----------------+---------+-----------------------+
Date_Time
是字符串格式。
然后我想把它变成这张表:
+----------------+---------+-------------+-------------+-------------+
| NIP | Nama | Date | In | Out |
+----------------+---------+-------------+-------------+-------------+
| 050803075200 | Teguh | 2013-02-21 | | 18:18:07 |
| 050803075200 | Teguh | 2013-02-20 | | 20:31:02 |
| 050803075201 | Supomo | 2013-02-20 | 07:45:57 | 17:24:13 |
| 050803075201 | Supomo | 2013-02-21 | 07:53:40 | 17:31:57 |
+----------------+---------+-------------+-------------+-------------+
什么查询会这样做?
答案 0 :(得分:0)
<强> Here is the SQLFiddel Demo 强>
以下是查询
select NIP,
Nama,
DATE_FORMAT(Date_Time, '%Y-%m-%d') as date,
Case Min(Date_Time)
When Max(Date_Time)
Then ''
Else DATE_FORMAT(Date_Time, '%H:%i:%s')
End as InTime,
DATE_FORMAT(max(Date_Time), '%H:%i:%s') as OutTime
from Attd
Group By NIP,
Nama,
DATE_FORMAT(Date_Time, '%Y-%m-%d')