Jump to content

MYSQL - Return next and previous row, based on variable


dumbnoob

Recommended Posts

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.