问题
I have attendance record table like this
+----------------+---------+-----------------------+
| 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 is in string format.
And then I want to make it like this table:
+----------------+---------+-------------+-------------+-------------+
| 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 |
+----------------+---------+-------------+-------------+-------------+
What query will do this?
回答1:
Here is the SQLFiddel Demo
Below is the Query
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')
来源:https://stackoverflow.com/questions/18227191/datetime-string-to-date-in-time-and-out-time