Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 使用unix时间戳在一个月内对SQL记录进行计数_Mysql_Sql_Datetime_Group By_Timestamp - Fatal编程技术网

Mysql 使用unix时间戳在一个月内对SQL记录进行计数

Mysql 使用unix时间戳在一个月内对SQL记录进行计数,mysql,sql,datetime,group-by,timestamp,Mysql,Sql,Datetime,Group By,Timestamp,我试图返回每个月内的记录计数,并按月/年对结果进行分组 架构如下所示: id title timestamp 我一直在四处寻找,但没有得到我所期望的结果。谢谢。格式化时间戳,然后按其分组 按月分组: SELECT DATE_FORMAT(t.timestamp, "%Y-%m") AS "_Month", COUNT(*) FROM yourtable as t GROUP BY _Month; 按年份分组: SELECT DATE_FORMAT(t.timestamp, "%Y

我试图返回每个月内的记录计数,并按月/年对结果进行分组

架构如下所示:

id    title    timestamp

我一直在四处寻找,但没有得到我所期望的结果。谢谢。

格式化时间戳,然后按其分组

按月分组:

SELECT DATE_FORMAT(t.timestamp, "%Y-%m") AS "_Month", COUNT(*)
FROM yourtable as t
GROUP BY _Month;
按年份分组:

SELECT DATE_FORMAT(t.timestamp, "%Y") AS "_Year", COUNT(*)
FROM yourtable as t
GROUP BY _Year;
如果时间戳字段存储为unixtime值,只需环绕该字段:

SELECT DATE_FORMAT(FROM_UNIXTIME(t.timestamp), "%Y") AS "_Year", COUNT(*)
FROM yourtable as t
GROUP BY _Year;

格式化时间戳,然后按其分组

按月分组:

SELECT DATE_FORMAT(t.timestamp, "%Y-%m") AS "_Month", COUNT(*)
FROM yourtable as t
GROUP BY _Month;
按年份分组:

SELECT DATE_FORMAT(t.timestamp, "%Y") AS "_Year", COUNT(*)
FROM yourtable as t
GROUP BY _Year;
如果时间戳字段存储为unixtime值,只需环绕该字段:

SELECT DATE_FORMAT(FROM_UNIXTIME(t.timestamp), "%Y") AS "_Year", COUNT(*)
FROM yourtable as t
GROUP BY _Year;

你想分别计算2010年1月和2011年1月,对吗?你想分别计算2010年1月和2011年1月,对吗?