mysql select records greater than 3 months

前端 未结 3 1342
生来不讨喜
生来不讨喜 2020-12-29 06:37

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

相关标签:
3条回答
  • 2020-12-29 06:53
    SELECT  *
    FROM    users
    WHERE   user_datecreated >= NOW() - INTERVAL 3 MONTH
    
    0 讨论(0)
  • 2020-12-29 06:55

    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)
    
    0 讨论(0)
  • 2020-12-29 07:07

    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);
    
    0 讨论(0)
提交回复
热议问题