我认为我在做一些愚蠢的事情,因为这个查询没有给我想要的输出:
我的 table 是这样的:
id integer,
current_date date
表格中的条目是:
1, 2016-08-24
2, 2016-08-25
3, 2016-08-26
4, 2016-07-21
5, 2016-07-22
查询:
select MONTH(current_date),count(*)
from test
group by MONTH(current_date)
此查询正在返回我:
8, 5
我期待的是:
7, 2
8, 3
最佳答案
current_date
是 MySQL reserved word返回今天的日期。要将其用作表格列,您需要将其放在反引号中。
select MONTH(`current_date`), COUNT(*)
from test
GROUP BY MONTH(`current_date`)
或者您可以将该列重命名为不与保留字冲突的名称。
关于MySQL 按月分组不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39177547/