I have a MySQL question. I have a table with three columns: 'salesdate' (contains a date), 'salestime' (contains time) and 'remaining' (contains integer). The table contains both past and future dates. I want to select a value from 'remaining' which corresponds most closely to the current date and time.
I have built the query below but it only selects values where both the date and time values are both true. This could produce the wrong result as there could be rows with yesterday's date in the 'salesdate', but all the 'salestime' values are greater than the current time - so my current query would ignore these...even if they are the closest to the current date/time.
My current query:
$result = mysql_query(
"SELECT remaining
FROM quantity_time
WHERE salesdate<=CURDATE()
AND salestime<=CURTIME()
ORDER BY salesdate DESC
LIMIT 1 ");
I think what I need to do is query for dates first and then somehow filter this list to show the date/time that is closest to the current time? but how ? If you can help - I've searched and can't find a clear, step by step way to do this. Your help is much appreciated.