I am trying to write a query to select all records from users
table where User_DateCreated
(datetime field) is >= 3 months from today.
Any ideas?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
SELECT * FROM users WHERE user_datecreated >= NOW() - INTERVAL 3 MONTH
Method 2
If you want to ignore the time of day when a user was created you can use the following. So this will show someone created at 8:00am if you run Quassnoi’s example query at 2:00pm.
SELECT * FROM users WHERE DATE(user_datecreated) >= DATE(NOW() - INTERVAL 3 MONTH)
Method 3
Using DATE(user_datecreated) prevents mysql from using any indexes on the column, making the query really slow when the table grows.
You don’t have to ignore the time when the user was created if you remove the time from the “3 months ago” date, as all the users created that day will match the condition.
SELECT * FROM users WHERE user_datecreated >= DATE(NOW() - INTERVAL 3 MONTH);
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0