Canman2005 Posted June 2, 2007 Share Posted June 2, 2007 Hi all I have a table in a SQL database called "webitems" which looks like ID TITLE LEVEL 1 home 65 2 about 234 3 contact 94 4 legal 21 5 services 157 What I want to do is to take an ID number, for example ID number 3 (contact) and return the next row in the LEVEL sequence, so for ID number 3, the LEVEL is 94, so it would return ID number 5 (services) as its the next number in the LEVEL sequence. If ID number 1 (home) was selected, then because its LEVEL value is 65, it would look for the next one in the sequence and return it, in this instance, ID number 3 would be returned, as the value in LEVEL is 94, which is the next in the LEVEL seqence. Does that make sense? If anyone can help, could you also tell me how can I also reverse it, so rather than the next LEVEL in the sequence, it would show the previous one in the sequence. For the next in sequence, I did try SELECT * FROM `webitems` WHERE `level` > '94' ORDER BY `level` ASC LIMIT 1 and SELECT * FROM `webitems` WHERE `level` < '94' ORDER BY `level` ASC LIMIT 1 for the reversed one But that seems to sometimes return an incorrect result, so I guess its wrong. Can anyone help? Thanks a million in advance Dave Quote Link to comment https://forums.phpfreaks.com/topic/53927-query-next-previous-in-sequence/ Share on other sites More sharing options...
AndyB Posted June 2, 2007 Share Posted June 2, 2007 SELECT * FROM `webitems` WHERE `level` < '94' ORDER BY `level` ASC LIMIT 1 Should be SELECT * FROM `webitems` WHERE `level` < '94' ORDER BY `level` DESC LIMIT 1 Quote Link to comment https://forums.phpfreaks.com/topic/53927-query-next-previous-in-sequence/#findComment-266629 Share on other sites More sharing options...
Canman2005 Posted June 2, 2007 Author Share Posted June 2, 2007 Cheers Andy Quote Link to comment https://forums.phpfreaks.com/topic/53927-query-next-previous-in-sequence/#findComment-266832 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.