php mysql选择大于3个月的记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2766116/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mysql select records greater than 3 months
提问by fire
I am trying to write a query to select all records from userstable where User_DateCreated(datetime field) is >= 3 months from today.
我正在尝试编写一个查询来从users表中选择所有记录,其中User_DateCreated(日期时间字段)从今天起 >= 3 个月。
Any ideas?
有任何想法吗?
回答by Quassnoi
SELECT  *
FROM    users
WHERE   user_datecreated >= NOW() - INTERVAL 3 MONTH
回答by Aaron W.
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.
如果您想忽略创建用户的时间,您可以使用以下内容。因此,如果您在下午 2:00 运行 Quassnoi 的示例查询,这将显示在上午 8:00 创建的某人。
SELECT  *
FROM    users
WHERE   DATE(user_datecreated) >= DATE(NOW() - INTERVAL 3 MONTH)
回答by ceteras
Using DATE(user_datecreated) prevents mysql from using any indexes on the column, making the query really slow when the table grows.
使用 DATE(user_datecreated) 可以防止 mysql 使用列上的任何索引,从而在表增长时使查询变得非常慢。
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.
如果从“3 个月前”日期中删除时间,则不必忽略创建用户的时间,因为当天创建的所有用户都将符合条件。
SELECT  *
FROM    users
WHERE   user_datecreated >= DATE(NOW() - INTERVAL 3 MONTH);

