我有一个简单的表(walkin2012):查询数据库显示具有以下字段的上午进行记录计数和下午
ID | date | store | timestamp | custName
1 | 2013-05-08 | Plainfield | 2013-05-08 10:38:40 | cust1
2 | 2013-05-08 | Plainfield | 2013-05-08 13:38:40 | cust2
3 | 2013-05-09 | Bayport | 2013-05-09 20:38:40 | cust3
4 | 2013-05-10 | Plainfield | 2013-05-10 17:38:40 | cust4
5 | 2013-05-10 | Bayport | 2013-05-10 11:38:40 | cust4
我想查询表,以显示记录的数经过10 AM-1PM为上午和什么促成的为下午每店日期,以便它只会给我这样的结果:
date | store | morning | afternoon
2013-05-08 | Plainfield | 1 | 1
2013-05-09 | Bayport | 0 | 1
2013-05-10 | Bayport | 1 | 0
2013-05-10 | Plainfield | 0 | 1
我得到的日期,商店,早上上班,但我对如何困惑子查询下午的部分。这里是我的查询到目前为止:
SELECT date, store, count(walkinID) AS Morning
FROM walkin2012
WHERE timestamp
BETWEEN concat_ws(' ',date,'10:00:00') AND concat_ws(' ',date,'13:00:00')
GROUP BY date,store
任何帮助,将不胜感激。非常感谢你。
请你创建一个相同的小提琴 – diEcho 2013-05-14 05:19:04
逻辑部分:' TIME(timestamp column)BETWEEN STR_TO_DATE('10AM','%l%p')AND STR_TO_DATE('1PM','%l%p'))作为上午' – diEcho 2013-05-14 05:44:20