我有一个“订单”表,其中有一个时间戳字段,因此数据如下所示:
id | createdate | customerid
1 | 2013-10-03 09:35:20 | 5
2 | 2013-10-03 09:35:20 | 9
3 | 2013-10-04 15:00:20 | 5
4 | 2013-10-05 12:15:20 | 5
所以从这张表中我需要创建一个html表,其中显示Mon-Saturday在顶部,小时在侧面,所以这样:
Hours | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday
9:30 - 10 | 0 | 0 | 0 | 2 | 0 | 0
10-11 | 0 | 0 | 0 | 0 | 0 | 0
11-12 | 0 | 0 | 0 | 0 | 1 | 0
所以到目前为止我所拥有的是:
SELECT DATE_FORMAT( createdate, '%m/%d/%Y' ) AS createdate
, DATE_FORMAT( createdate, '%H' ) AS createhour, COUNT( * ) AS totals
FROM orders
WHERE createdate >= CURDATE( ) - INTERVAL DAYOFWEEK( CURDATE( ) ) +6
DAY AND createdate < CURDATE( ) - INTERVAL DAYOFWEEK( CURDATE( ) ) -1
DAY GROUP BY DATE_FORMAT( createdate, '%m/%d/%Y' ) , DATE_FORMAT( createdate, '%H' )
这会返回数据但不是我需要的格式,有没有办法在mysql中执行?或者它是否涉及一些带有数组的PHP工作?