关于sql:MySql选择并计算每天的数据数量

MySql Select and count number of data per day

我有一个带列的下表:

1
id    |    data    |    date

如何查询日期范围从1到30天的所有数据并每天统计所有数据?

我试过了,但它几秒钟后也会出现。

1
2
3
4
Select count(data) as data
From table
Where date between '2013-12-02 00-00-00' and '2014-01-02 23:59:59'
group by date

我想实现

1
2
day 1  |  day 2  |  day  | day 4  | . . .
 3     |   4     |   2   |    5   | . . .


尝试以下操作

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
SELECT @count:=0;
SELECT CONCAT('day ',@count+1) as day, COUNT(*), date
FROM date between
  '2013-12-02 00-00-00'
AND
  '2014-01-02 23:59:59')
GROUP BY date