views:

32

answers:

2

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.

A: 

add a condition WHERE ID<>MY_LAST_ID. This can not work with triple and more same dates, so you can collect already taken ID's to array like (4,5,6) - see array_push(), implode it with "," to convert to a string (let's call it YOUR_IDS_STRING) and add to your query:

WHERE id NOT IN( YOUR_IDS_STRING )

And after each query make check, does date has changed and if it does - you can unset your array and start from begining (this is not neccesary, but gives you more performance, because YOUR_ID_STRING will be only that long as is need).

If your page is refreshing between queries, maybe try to push YOUR_ID_STRING in session variable, _GET or cookies, and simply concat next id's by operator .=

killer_PL
A: 

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
Mark Byers
I have a data set like this:[ 45 | 2011-01-14 ] [ 23 | 2011-01-22 ] [ 48 | 2011-01-23 ] [ 25 | 2011-01-23 ][ 63 | 2011-02-19 ] It goes 45,23,25.. skips over 48.Any other ideas??
Phil
@Phil: I have tested it on the test data you provided in your comment with $iid = 25 and it works fine, returning the record with id = 48. Try running the supplied test code in my updated answer. You can run it in MySQL Query Browser to see that it works for yourself. I think your problem must lie elsewhere in your code.
Mark Byers
Mark,You are correct! it does work. Thank you very much! you saved alot of hair that will now stay on my head ;)
Phil