next and previous data of MySQL

[expired user #2767]'s profile image [expired user #2767] posted 17 years ago in General Permalink
Hi All. I have a table called 'note' . In the 'note' table, I have id , subject and content . The id is set on autoincrement and it's of int format.

I need to find the next and previous of the current id . E.g , current id = 7.
I need to find the next and the previous of id 7. How do I do it .

One more thing , in autoincrement , if you delete off one id , it would not rearrange itself into the blank space. e.g. id 4,5,6,7,10 .If I delete off id 5,
then I would have 4,6,7,10 left in the table. If I use 'select * from note where id= ? ++;' and if the current id is 4 , the next id is 5 but 5 have been deleted or if the current id is 7 , there is no id 8 or 9 but only 10.

So essentially, how do I write the code for finding the next and previous ?

I am also using JDBC for my Java Application for writing this codes above.
[expired user #2778]'s profile image [expired user #2778] posted 17 years ago Permalink
PREVIOUS:
[list:b29315f582]
[*:b29315f582] SELECT id FROM note WHERE id<7 ORDER BY nid DESC LIMIT 1;
[/list:u:b29315f582]

NEXT:
[list:b29315f582]
[*:b29315f582] SELECT id FROM note WHERE id>7 ORDER BY nid LIMIT 1;
[/list:u:b29315f582]
[expired user #2767]'s profile image [expired user #2767] posted 17 years ago Permalink
Thanks Mike. It worked. :D
[expired user #2778]'s profile image [expired user #2778] posted 17 years ago Permalink

Thanks Mike. It worked. :D



No problem. :P

Please login to leave a reply, or register at first.