Home > Database > Mysql Tutorial > How to Group Data by Date from a DATETIME Field in MySQL?

How to Group Data by Date from a DATETIME Field in MySQL?

Linda Hamilton
Release: 2024-10-31 15:52:01
Original
721 people have browsed it

How to Group Data by Date from a DATETIME Field in MySQL?

Casting DATETIME as DATE in MySQL

Casting DATETIME as DATE is necessary when you want to group data by date in a database query, but the database only stores a DATETIME field. The DATE() function in MySQL allows you to achieve this by extracting only the date portion from a DATETIME column.

Original Query Error

The original query provided:

select * from follow_queue group by follow_date cast follow_date as date
Copy after login

fails to work because the cast follow_date as date syntax is incorrect.

Solution: Using the DATE() Function

To correctly cast DATETIME as DATE, use the DATE() function as follows:

select * from follow_queue group by DATE(follow_date)
Copy after login

The DATE() function accepts a DATETIME expression as its argument and returns only the date portion. By grouping the results by the DATE(follow_date) expression, you can effectively group the entries by date, even though the database stores the timestamp in a DATETIME format.

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

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template