Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

sql question?

lets say i have a table in sql.. with 3 records

u = unknown

id -------------- name

u u

13 moose

u u

how do i write the write quary to select those 3 rows? i know only the middle record and i need to query 1 record that comes before.. and for 1 record that comes after

the id is not constant so its impossible to use the ++

Update:

heres the table again.. sorry

u = unkown

id---------name|

--------------------|

u----------u-------|

3--------moose-|

u----------u-------|

Update 2:

Dumitru.. i cant do that because my my id is not constant.. so.. if my id is 3.. it does not mean that the next record is 4

Update 3:

oki i will expand a little.. i have a photo album.. in which every picture has a id .. the id is on auto increment.. theres also a possibility to delete the picture.. so i cant relay on adding/subtracting one number from my initial id (which is known).. im just thinking whats the easy way to do it.. while waiting for the answer i wrote a php script to get this information.. using 2 quary and fetching all to array..however, this seems too much.. especially when this is done every time a page is generated.also.. i cant put the array into session.. because the contents of the album always change.. and if i keep an outdated array in the session.. the user will not be able to see the newly added pictures without revisiting the album

4 Answers

Relevance
  • Anonymous
    1 decade ago
    Favorite Answer

    Assuming you know you want to retrieve a specific record number, then getting the adjacent items is simple if you use UNION (I am assuming you are using MySQL):

    $idvalue = 27

    SELECT * FROM table WHERE id = $idvalue

    UNION

    SELECT * FROM table WHERE id < $idvalue ORDER BY id DESC LIMIT 1

    UNION

    SELECT * FROM table WHERE id > $idvalue ORDER BY id ASC LIMIT 1

    This query shouldn't be affected at all by adding or deleting records.

  • 1 decade ago

    Provide a little more details for us please - will there only every be those 3 records (rows) or are you needing to do this more generically? In other words for some reason you need to be able to look up any given "name" and then also find the record immediately before & after it?

    If you simply need *all* of the records, just do:

    SELECT id, name FROM my_table

    Not sure how to do it more generically with SQL. Could you at least count on the ID of the preceding record to be less than the known one and the one after it to be larger? If this is the case you could try this series of queries:

    SELECT id FROM my_table WHERE name = 'moose';

    store the returned id in a variable ($id)

    To get the previous record do:

    SELECT max(id), name FROM my_table WHERE id < $id;

    To get the next record do

    SELECT min(id), name FROM my_table WHERE id > $id;

    The above should at least work assuming that the preceding record you're after has the _next_ smallest id (it wouldn't matter if it was 1 smaller or 13 smaller) . Same for the following record, it's just assumed to be the next larger id.

    If you must have it in a single SQL query, you could use sub-queries or UNION - the exact details will depend on which database (MySQL, PostgreSQL, Oracle, MS SQL, SQLite ...)

  • 1 decade ago

    This should work, but it is a little clumsy

    select top 1 id, name from myTable

    where id < (select id from myTable where name = 'moose')

    order by id desc

    union

    select id, name from myTable where name = 'moose'

    union

    select top 1 id, name from myTable

    where id > (select id from myTable where name = 'moose')

    order by id asc

  • 1 decade ago

    If you're using PHP + MySQL, you can do it in 2 queries.

    First one:

    $result = mysql_query("SELECT * FROM `tableName` WHERE `name` = 'moose' LIMIT 1")

    and then

    $id1 = $result['id']--;

    $id2 = $result['id'];

    $id3 = $result['id']++;

    $result2 = mysql_query("SELECT * FROM `tableName` WHERE `id` IN($id1,$id2,$id3)");

    I might have made some mistakes in the syntax, but the idea should be clear.

Still have questions? Get your answers by asking now.