Phil Phil - 7 months ago 9
PHP Question

Forward Back Records in MySQL with the same DATA in the primary

I have a table that is is sorted 1st by Reminder Date then ID

Table Looks like:

ID | remind_date
1 2011-01-23
2 2010-02-21
4 2011-04-04
5 2011-04-04
6 2009-05-04


I am using a PHP front end to move forward and back thur the records. I want to have forward and back buttons but i am running into a problem with the 2 reminder dates that are the same.

Just to note the ID's are NOT in order, they are here but in the actual database they are mixed up when sorting by reminder_date

The select statement i am using is: ($iid is the current record i am on)

SELECT id FROM myDB.reminders where remind_date > (SELECT remind_date FROM myDB.reminders where id=$iid) order by remind_date ASC LIMIT 1


So what happens when i get to the dates that are the same its skips over one because its asking for remind_date >.

If i use remind_date >= it returns the current record. My solution was then to use limit 2 and check via PHP to if the 1st record = my current ID, if it did use the next one. but what it there are 3 dates the same or 4 etc..

I also thought about using the ID field but since they are out of order i can't add in a ID > $iid.

Any ideas? it works great except for 2 dates that are the same.

Answer

You might be able to use this:

SELECT ID, remind_date
FROM
(
    SELECT @prev_id := -1
) AS vars
STRAIGHT_JOIN
(
    SELECT
        ID,
        remind_date,
        @prev_id AS prev_id,
        @prev_id := id
    FROM myDB.reminders
    ORDER BY remind_date, ID
) T1
WHERE prev_id = $iid

Here is a test of the above with your test data from your comment:

CREATE TABLE Table1 (ID INT NOT NULL, remind_date DATE NOT NULL);
INSERT INTO Table1 (ID, remind_date) VALUES
(45, '2011-01-14'),
(23, '2011-01-22'),
(48, '2011-01-23'),
(25, '2011-01-23'),
(63, '2011-02-19');

SELECT ID, remind_date
FROM
(
    SELECT @prev_id := -1
) AS vars
STRAIGHT_JOIN
(
    SELECT
        ID,
        remind_date,
        @prev_id AS prev_id,
        @prev_id := id
    FROM table1
    ORDER BY remind_date, ID
) T1
WHERE prev_id = 25

Result:

ID  remind_date
48  2011-01-23
Comments