简体   繁体   English

MySQL 按天分组,带有 TIMESTAMP 列

[英]MySQL group by day with TIMESTAMP column

I have a mysql table that has a column for "date time" in TIMESTAMP format.我有一个 mysql 表,其中有一列 TIMESTAMP 格式的“日期时间”。 Is there a way to group the rows by day using that column?有没有办法使用该列按天对行进行分组? And in an SQL query, not grouping them in php.在 SQL 查询中,不将它们分组在 php 中。

Assuming your column is one of the Date and Time types :假设您的列是Date 和 Time 类型之一:

SELECT ... GROUP BY DATE(`datetimecolumn`)
GROUP BY TO_DAYS(`datetime_column`)

If you mean you have a standard DATETIME column, you can group by using one of the functions described here .如果您的意思是您有一个标准的 DATETIME 列,您可以使用此处描述的函数之一进行分组。

For instance:例如:

GROUP BY DATE(datetime_column);

If you actually have a TEXT or VARCHAR column (your answer doesn't specify), then you'll need to convert it to to a date first:如果您实际上有一个 TEXT 或 VARCHAR 列(您的答案没有指定),那么您需要先将其转换为日期:

GROUP BY DATE(STR_TO_DATE(datetime_column));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM