MySQL Query to Select Data from Today Based on Datetime
In MySQL, you can retrieve data from a specific date by utilizing the DATE() and CURDATE() functions. The DATE() function extracts the date component from a datetime value, while CURDATE() returns the current system date.
Problem
A user needs to select data from a table where the datetime field signup_date matches today's date, but their initial query using CURDATE() and DATE_FORMAT() is not working.
Solution
To correctly compare a datetime value to today's date, you should use:
SELECT users.id, DATE_FORMAT(users.signup_date, '%Y-%m-%d') FROM users WHERE DATE(users.signup_date) = CURDATE()
In the modified query:
By comparing the date components of signup_date and CURDATE(), you can filter out records that do not match today's date.
The above is the detailed content of How to Select Data from Today's Date Using the `signup_date` DateTime Field in MySQL?. For more information, please follow other related articles on the PHP Chinese website!