next and previous data of MySQL
| User, date | Message |
|---|---|
|
Written by Thotheolh
6 years ago Category: General 4 posts since Sun, 29 Jul 07 |
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. |
|
Written by MikeSchinkel
6 years ago 13 posts since Sat, 11 Aug 07 |
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] |
|
Written by Thotheolh
6 years ago 4 posts since Sun, 29 Jul 07 |
Thanks Mike. It worked. :D |
|
Written by MikeSchinkel
6 years ago 13 posts since Sat, 11 Aug 07 |
Thotheolh wrote: Thanks Mike. It worked. :D No problem. :P |
|
Please login to leave a reply, or register at first. |