fire
fire

Reputation: 21531

mysql select records greater than 3 months

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?

Upvotes: 24

Views: 65883

Answers (3)

ceteras
ceteras

Reputation: 3378

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);

Upvotes: 6

Aaron W.
Aaron W.

Reputation: 9299

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)

Upvotes: 17

Quassnoi
Quassnoi

Reputation: 425813

SELECT  *
FROM    users
WHERE   user_datecreated >= NOW() - INTERVAL 3 MONTH

Upvotes: 88

Related Questions