tags:

views:

141

answers:

3

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? Thanks!

+4  A: 
SELECT  *
FROM    users
WHERE   user_datecreated >= NOW() - INTERVAL 3 MONTH
Quassnoi
+3  A: 

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)
Aaron W.
A: 

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