php SQL 选择日期早于今天的记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9358688/
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
SQL selecting records with dates before today
提问by David
I have a mysql DB with tables, of which in the one table I have a date type field, I want the most recently passed date - so I want it to order by dates descending, but only take records from before today, and then take only the top most one using the LIMIT function, and also there is the addition of the WHERE clause being that the offer must be for the selected city.
我有一个带有表的 mysql 数据库,其中一个表中我有一个日期类型字段,我想要最近通过的日期 - 所以我希望它按日期降序排序,但只获取今天之前的记录,然后取仅使用 LIMIT 函数的最顶层,并且还添加了 WHERE 子句,即优惠必须针对所选城市。
$result = mysql_query("
SELECT * FROM offers
WHERE city = ".$_SESSION["city"]."
ORDER BY exp_date DESC
LIMIT 0, 1");
回答by Haim Evgi
ADD another condition to where clause
向 where 子句添加另一个条件
$result = mysql_query("
SELECT * FROM offers
WHERE city = ".$_SESSION["city"]." and Date < CURRENT_DATE()
ORDER BY exp_date DESC
LIMIT 1");
回答by David
SELECT * FROM deals WHERE city = 2 AND exp_date < CURDATE()
ORDER BY exp_date DESC LIMIT 0, 1
回答by Alexey Berezkin
Add the following condition to Where:
将以下条件添加到 Where:
... and exp_date < CURDATE()
... and exp_date < CURDATE()
See http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html.
请参阅http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html。