如何根据日期计算日期范围内特定列中的所有值

时间:2017-03-08 13:34:15

标签: mysql date

我有一个列有列:

ID| rect_id| vieweed_user_id| view_count|view_date
1 | 1      | 1              | 1         |2017-03-07 10:54:21
2 | 1      | 2              | 1         |2017-03-08 01:00:45
3 | 1      | 3              | 1         |2017-03-08 12:54:12

我想根据view_date找到所有view_count值的计数,例如

view_date  | view_count
2017-03-08 |    2
2017-03-07 |    1

但我的问题是我想在指定的日期范围之间找到所有已计算的view_count值,例如

  

2016-03-08至2017-03-08

2 个答案:

答案 0 :(得分:2)

试试这个。

SELECT view_date, sum(view_count) as total_count FROM TABLE_NAME WHERE view_date >= '2016-03-08' AND view_date <= '2017-03-08' group by view_date

答案 1 :(得分:0)

您需要在日期和日期之间进行过滤,以获得所需的输出

select view_date,sum(view_count) from TABLE_NAME where view_date between ('2016-03-08' and '2017-03-08') group by view_date