Home >Database >Mysql Tutorial >How to Group MySQL Entries by Date from a DATETIME Column?

How to Group MySQL Entries by Date from a DATETIME Column?

Susan Sarandon
Susan SarandonOriginal
2024-10-30 21:52:30927browse

How to Group MySQL Entries by Date from a DATETIME Column?

Casting DATETIME to DATE in MySQL

Question:

When attempting to group database entries by date, despite having a datetime field, the following query fails:

select * from follow_queue group by follow_date cast follow_date as date

Answer:

To convert a DATETIME field to a DATE value for grouping, utilize the DATE() function:

select * from follow_queue group by DATE(follow_date)

The DATE() function extracts the date component from a DATETIME field, removing the time portion, and returning the result as a DATE value. This allows you to group entries by date, even if the underlying data is stored as datetime.

The above is the detailed content of How to Group MySQL Entries by Date from a DATETIME Column?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn