Help - Search - Members - Calendar
Full Version: SQL - select a record, and the one before and after
Invision Power Services > Community Forums > Community Web Design and Coding
blush
I have a bunch of records I want to show, but for comparison I also want to show the records preceding and following each one - is that possible?
elj
In three queries, it's pretty easy - but I don't know about just in one.
blush
well how do you do it in three queries? tongue.gif

Something else that might help is that each record has a sample number (the data in question is drill samples being assayed), and they are almost always in that order. There are a few exceptions... for example if the driller switched to a different sequence in the middle of a drill hole, or if the record I'm looking for is the last sample in the drill hole. But USUALLY they are in the same order as the sample number indicates. Sample number is generally a letter and six numbers but again, there are exceptions.
elj
If they have an ID or unique numeric field, then I guess you can just find out the ID of the main record, minus or plus one accordingly, and then select the other information using those IDs.

Sorry, did that make any sense? pinch.gif
Starnox
Or if they have a timestamp you could get it to display the next biggest and smallest time and limit the result to 1.
blush
Nope, the database design is pisspoor and they do not have a timestamp, nor is the identifying number ALWAYS the correct basis for ordering - though it usually is. If it's too much of a hassle we could assume for this purpose that the id number is always in the right order and accept a couple errors.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.