使用mysql从UTC格式计算特定月份

时间:2018-02-21 11:02:16

标签: mysql sql

我正在尝试从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

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