I've tried looking up other help for this problem but I'm just not getting it. Suppose I have a table that looks like the one below.
+----+--------------+------------+
| id | date_col | label |
+----+--------------+------------+
| 1 | 2010-09-07 | Record 1 |
| 2 | 2010-09-03 | Record 2 |
| 3 | 2010-08-23 | Record 3 |
| 4 | 2010-08-23 | Record 4 |
| 5 | 2010-08-23 | Record 5 |
| 6 | 2010-08-12 | Record 6 |
| 7 | 2010-08-06 | Record 7 |
| 8 | 2010-08-06 | Record 8 |
| 9 | 2010-08-02 | Record 9 |
| 10 | 2010-08-01 | Record 10 |
+----+--------------+------------+
When queried, I order these records according to date_col
and use id
(or really any other arbitrary column) to help ordering with duplicate dates.
mysql_query("SELECT * FROM table ORDER BY date_col DESC, id DESC");
However, when I query only one of these records at a time, I want to have a previous and a next button to navigate to the next or previous record. My problem is that date_col
allows duplicate values and so, for example, the query below does not work for me when determining the next record in sequence. (assume that this_date
is the date_col value and this_id
is the id value for the current record we are looking at)
mysql_query("SELECT id FROM table WHERE date_col > this_date ORDER BY date_col DESC, id DESC LIMIT 1");
Even this wouldn't work for me:
mysql_query("SELECT id FROM table WHERE date_col > this_date AND NOT id=this_id ORDER BY date_col DESC, id DESC LIMIT 1");
So what I'm going for is something like this - If I'm looking at the record with id #4, since its being ordered by date_col DESC id DESC
, the previous record should be id #5 and the next record should be id #3, but I'm not getting these results at all.
Can someone explain how to make this work properly? Any help is much appreciated.