David
David

Reputation: 4007

SQL selecting records with dates before today

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.

$result = mysql_query("
SELECT * FROM offers 
WHERE city = ".$_SESSION["city"]." 
ORDER BY exp_date DESC 
LIMIT 0, 1");   

Upvotes: 10

Views: 58695

Answers (3)

David
David

Reputation: 4007

SELECT * FROM deals WHERE city = 2 AND exp_date < CURDATE() 
ORDER BY exp_date DESC LIMIT 0, 1

Upvotes: 9

Alexey Berezkin
Alexey Berezkin

Reputation: 1543

Add the following condition to Where:

... and exp_date < CURDATE()

See http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html.

Upvotes: 3

Haim Evgi
Haim Evgi

Reputation: 125644

ADD another condition to where clause

$result = mysql_query("
SELECT * FROM offers 
WHERE city = ".$_SESSION["city"]."  and Date < CURRENT_DATE()
ORDER BY exp_date DESC 
LIMIT 1");

Upvotes: 17

Related Questions