MySql Select and count number of data per day
我有一个带列的下表:
如何查询日期范围从1到30天的所有数据并每天统计所有数据?
我试过了,但它几秒钟后也会出现。
1 2 3 4 |
我想实现
尝试以下操作
1 2 3 4 5 6 | SELECT DATE_FORMAT(date, '%Y-%m-%d') AS the_date , COUNT(*) AS count FROM table WHERE date BETWEEN DATE_FORMAT('2014-11-28', '%Y-%m-%d') AND DATE_FORMAT('2014-12-05', '%Y-%m-%d') GROUP BY the_date |
似乎您需要如下查询:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | SELECT CASE date_format(date ,"%W") WHEN 'Monday' THEN IFNULL((SELECT COUNT(id) FROM YOUR_TABLE WHERE date_format(date ,"%W") ='Monday' ) ,0) ELSE 0 END AS 'Monday', CASE date_format(date ,"%W") WHEN 'Tuesday' THEN IFNULL((SELECT COUNT(id) FROM YOUR_TABLE WHERE date_format(date ,"%W") ='Tuesday' ) ,0) ELSE 0 END AS 'Tuesday', CASE date_format(date ,"%W") WHEN 'Wednesday' THEN IFNULL((SELECT COUNT(id) FROM YOUR_TABLE WHERE date_format(date ,"%W") ='Wednesday' ) ,0) ELSE 0 END AS 'Wednesday', CASE date_format(date ,"%W") WHEN 'Thursday' THEN IFNULL((SELECT COUNT(id) FROM YOUR_TABLE WHERE date_format(date ,"%W") ='Thursday' ) ,0) ELSE 0 END AS 'Thursday', CASE date_format(date ,"%W") WHEN 'Friday' THEN IFNULL((SELECT COUNT(id) FROM YOUR_TABLE WHERE date_format(date ,"%W") ='Friday' ) ,0) ELSE 0 END AS 'Friday', CASE date_format(date ,"%W") WHEN 'Saturday' THEN IFNULL((SELECT COUNT(id) FROM YOUR_TABLE WHERE date_format(date ,"%W") ='Saturday' ) ,0) ELSE 0 END AS 'Saturday', CASE date_format(date ,"%W") WHEN 'Sunday' THEN IFNULL((SELECT COUNT(id) FROM YOUR_TABLE WHERE date_format(date ,"%W") ='Sunday' ) ,0) ELSE 0 END AS 'Sunday' FROM YOUR_TABLE |
1 2 3 4 5 6 7 |