I have a table with records belonging to different users.
Table:
RECORD_ID USER_ID DATE_CREATED
1 5 08/05/12
2 5 08/04/12
3 10 08/05/12
4 10 08/02/12
5 10 08/01/12
Query:
SELECT user_id, MIN(date_created) as date_created
FROM (entries)
GROUP BY user_id
The query returns the oldest record for each users.
RECORD_ID USER_ID DATE_CREATED
2 5 08/04/12
5 10 08/01/12
How can I select the oldest record nearest to a given date but not older? Something along the lines of…
SELECT oldest record > given oldest record
WHERE oldest record NOT =< given oldest record
For example a query returns all records for user 10:
RECORD_ID USER_ID DATE_CREATED
3 10 08/05/12
4 10 08/02/12
5 10 08/01/12
The given date for user 10 is 08/02/12. Now I have to select the next oldest record above 08/02/12.
That would be 08/05/12 and never records below that unless it reached the top. How is this possible in MYSQL?
Let
$date_givenhave the given date.