我正在尝试从yyyy-mm-dd中选择特定月份的计数声明hh:mm:ss
DateTime
-------------------
2018-01-20 14:31:07
2018-01-20 14:09:07
2018-02-20 12:02:07
2018-02-20 11:31:07
我的初始代码是,
SELECT COUNT(*) AS count FROM table WHERE DateTime LIKE '%Jan%'
我的预期结果
Jan = 2
Feb = 2
答案 0 :(得分:2)
使用YEAR()
和MONTH()
SELECT YEAR(`DateTime`) AS `YEAR`,
MONTH(`DateTime`) AS `MONTH`,
COUNT(*) AS count
FROM tableName
GROUP BY YEAR(`DateTime`),
MONTH(`DateTime`)
这里是Demo。
答案 1 :(得分:0)
SELECT MONTH(DateTime), COUNT(*) AS count FROM table GROUP BY 1