我有一组像这张表这样的数据:
ID sTime sDate Pin Name
29 5/4/2015 7:07:05 AM 5/4/2015 118 Sigit
30 5/4/2015 7:07:08 AM 5/4/2015 118 Sigit
31 5/4/2015 7:12:31 AM 5/4/2015 184 Aprilia
32 5/4/2015 7:13:20 AM 5/4/2015 182 Siska
33 5/4/2015 7:17:12 AM 5/4/2015 102 Sugandi
34 5/4/2015 7:17:14 AM 5/4/2015 102 Sugandi
35 5/4/2015 7:17:28 AM 5/4/2015 29 Ade
36 5/4/2015 7:17:33 AM 5/4/2015 164 Hendrik
37 5/4/2015 7:17:37 AM 5/4/2015 104 Rahmat
38 5/4/2015 7:17:41 AM 5/4/2015 67 Endang
39 5/4/2015 7:17:45 AM 5/4/2015 43 Dedy
40 5/4/2015 7:17:47 AM 5/4/2015 43 Dedy
41 5/4/2015 7:18:16 AM 5/4/2015 12 Toni
42 5/4/2015 7:19:16 AM 5/4/2015 115 Dedi
43 5/4/2015 7:20:14 AM 5/4/2015 30 Dadang
44 5/4/2015 7:21:58 AM 5/4/2015 165 Muammad
45 5/4/2015 7:22:00 AM 5/4/2015 165 Muammad
46 5/4/2015 7:22:02 AM 5/4/2015 165 Muammad
47 5/4/2015 7:22:04 AM 5/4/2015 165 Muammad
151 5/4/2015 4:03:53 PM 5/4/2015 43 Dedy
152 5/4/2015 4:03:58 PM 5/4/2015 164 Hendrik
153 5/4/2015 4:04:04 PM 5/4/2015 26 Syamsuddin
154 5/4/2015 4:04:09 PM 5/4/2015 26 Syamsuddin
155 5/4/2015 4:04:21 PM 5/4/2015 19 Eddy
156 5/4/2015 4:04:32 PM 5/4/2015 133 Haetami
157 5/4/2015 4:04:38 PM 5/4/2015 92 Budi
158 5/4/2015 4:05:05 PM 5/4/2015 131 Rosadi
159 5/4/2015 4:05:29 PM 5/4/2015 115 Dedi
160 5/4/2015 4:05:48 PM 5/4/2015 80 Cahya
161 5/4/2015 4:05:54 PM 5/4/2015 125 Yahya
每个名称和每一天的条件:
所选数据:
ID sTime sDate Pin Name
29 5/4/2015 7:07:05 AM 5/4/2015 118 Sigit
31 5/4/2015 7:12:31 AM 5/4/2015 184 Aprilia
32 5/4/2015 7:13:20 AM 5/4/2015 182 Siska
33 5/4/2015 7:17:12 AM 5/4/2015 102 Sugandi
35 5/4/2015 7:17:28 AM 5/4/2015 29 Ade
36 5/4/2015 7:17:33 AM 5/4/2015 164 Hendrik
37 5/4/2015 7:17:37 AM 5/4/2015 104 Rahmat
38 5/4/2015 7:17:41 AM 5/4/2015 67 Endang
39 5/4/2015 7:17:45 AM 5/4/2015 43 Dedy
41 5/4/2015 7:18:16 AM 5/4/2015 12 Toni
42 5/4/2015 7:19:16 AM 5/4/2015 115 Dedi
43 5/4/2015 7:20:14 AM 5/4/2015 30 Dadang
44 5/4/2015 7:21:58 AM 5/4/2015 165 Muammad
151 5/4/2015 4:03:53 PM 5/4/2015 43 Dedy
152 5/4/2015 4:03:58 PM 5/4/2015 164 Hendrik
154 5/4/2015 4:04:09 PM 5/4/2015 26 Syamsuddin
155 5/4/2015 4:04:21 PM 5/4/2015 19 Eddy
156 5/4/2015 4:04:32 PM 5/4/2015 133 Haetami
157 5/4/2015 4:04:38 PM 5/4/2015 92 Budi
158 5/4/2015 4:05:05 PM 5/4/2015 131 Rosadi
159 5/4/2015 4:05:29 PM 5/4/2015 115 Dedi
160 5/4/2015 4:05:48 PM 5/4/2015 80 Cahya
161 5/4/2015 4:05:54 PM 5/4/2015 125 Yahya
并显示如下表:
Name Date In Time Out Time
Sigit 5/4/2015 7:07:05
Aprilia 5/4/2015 7:12:31
Dedy 5/4/2015 7:17:45 16:03:53
Dedi 5/4/2015 7:19:16 16:05:29
Cahya 5/4/2015 16:05:48
Yahya 5/4/2015 16:05:54
我已经抓过这样的任何查询,但我找不到确切的解决方案。
任何帮助将不胜感激。谢谢!
答案 0 :(得分:0)
这假定sTime
是您显示的格式的字符串。
SELECT name, date, IFNULL(intime, '') `In Time`, IFNULL(outtime, '') `Out Time`
FROM (
SELECT name, sdate AS date,
MIN(IF(SUBSTRING_INDEX(sTime, ' ', -2) BETWEEN '7:00:00 AM' AND '8:30:00 AM',
SUBSTRING_INDEX(SUBSTRING_INDEX(sTime, ' ', 2), ' ', -1),
NULL)) AS intime,
MAX(IF(SUBSTRING_INDEX(sTime, ' ', -2) BETWEEN '4:00:00 PM' AND '5:15:00 PM',
SUBSTRING_INDEX(SUBSTRING_INDEX(sTime, ' ', 2), ' ', -1),
NULL)) AS outtime
FROM YourTable
GROUP BY name, sdate
HAVING intime IS NOT NULL OR outtime IS NOT NULL
) AS x
ORDER BY intime IS NULL, intime, outtime