Heim  >  Artikel  >  Datenbank  >  mysql 将字段time按天/月/年分组

mysql 将字段time按天/月/年分组

WBOY
WBOYOriginal
2016-06-07 16:29:02983Durchsuche

假设表中有一个字段time,格式为Unix时间戳,现需要按照该字段统计每天的记录数。 方法: 利用convert函数convert(char(10),time,120) as time,然后group by time即可。 原理:convert函数将时间戳转换成ODBC标准时间(120参数指定),然后取前十位,则刚好得

假设表中有一个字段time,格式为Unix时间戳,现需要按照该字段统计每天的记录数。

方法:

利用convert函数convert(char(10),time,120) as time,然后group by time即可。

原理:convert函数将时间戳转换成ODBC标准时间(120参数指定),然后取前十位,则刚好得到完整日期(不含时分秒),再分组即可。

按月,按年分组方法一样,只需要修改convert()方法第一个参数的长度。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn