dumbnoob Posted January 12, 2010 Share Posted January 12, 2010 Ok, I am having some issues trying to work out what my query should be when trying to achieve the following, but first a quick explanation. I have a script, which when run, extracts information from a webpage, and submits it to a php form, that dumps it neatly into a table in my database. Now, I am trying to write a tool to extract the information I need, in a specific way, where I'm not requesting a specific line. For the sake of what I'm doing. My Table has four fields. ID, subid, TIME, DATE when my initial script is run, it exracts the last three fields from the page its run from, and places them in the database. Now, I have another tool, that extracts the subid data from a page, and I want to query the database, to return the next greater value, and the previous lesser value. I struggle to explain things, so I will do something semi graphical. ID subid TIME DATE 1 1287348 00:00:01 1/1/10 2 1297648 01:15:00 1/1/10 3 2118041 16:09:45 7/2/10 4 2131005 17:19:42 7/2/10 5 2610732 22:58:56 8/2/10 Now, my second script will be returning a subid, that will not be equal to any entries in the database. For example, it may return 2433999. Now, I want to be able to query the database, for the last subid number to be less than that, and the first subid number to be greater, that is to say, I want it to return lines, 4 and 5 from my example table. ( 2131005 17:19:42 7/2/10 2610732 22:58:56 8/2/10 ) or if my script submitted 1289143 it should return the first two lines. ( 1 1287348 00:00:01 1/1/10 2 1297648 01:15:00 1/1/10 ) Is there anyone here who could advise me as to what my query would need to be. and possibly a way of returning only a single entry, if the subid matches exactly. Thank you for your time. Quote Link to comment https://forums.phpfreaks.com/topic/188150-mysql-return-next-and-previous-row-based-on-variable/ Share on other sites More sharing options...
cags Posted January 12, 2010 Share Posted January 12, 2010 Something along the lines of this should work... SELECT subid FROM tablename WHERE subid > $subid ORDER BY subid ASC LIMIT 1 SELECT subid FROM tablename WHERE subid < $subid ORDER BY subid DESC LIMIT 1 Quote Link to comment https://forums.phpfreaks.com/topic/188150-mysql-return-next-and-previous-row-based-on-variable/#findComment-993472 Share on other sites More sharing options...
dumbnoob Posted January 15, 2010 Author Share Posted January 15, 2010 Thanks Cags, by chance I found out someone I know knows alot more about php than I was aware, and they helped me out, by giving almost the exact same answer as you have. Quote Link to comment https://forums.phpfreaks.com/topic/188150-mysql-return-next-and-previous-row-based-on-variable/#findComment-995523 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.