mysql fetch next previous record

(SELECT * FROM zip WHERE id > 26 ORDER BY id ASC LIMIT 1)
UNION
(SELECT * FROM zip WHERE id = 26 ORDER BY id DESC LIMIT 1)
UNION
(SELECT * FROM zip WHERE id < 26 ORDER BY id DESC LIMIT 1)
order by id

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Live
  • MySpace
  • Reddit
  • RSS
  • StumbleUpon
  • Technorati
  • Tumblr
  • Twitter
  • Yahoo! Buzz

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

3 Comments »

 
  • hitesh says:

    The other alternative for the above query is posted below. its much better and simpler then the one given above.

    Its simple to use. Just one lined.

    select id from zip_code where id = (select min(id) from zip_code where id > 26) || id = (select max(id) from zip_code where id < 26) || id = 26

  • vivek says:

    any other post regarding next previous select query is much appreciated…

  • mahesh says:

    The other alternative for the above query is posted below. its much better and simpler then the one given above.

    Its simple to use. Just one lined.

    select id from zip_code where id = (select min(id) from zip_code where id > 26) || id = (select max(id) from zip_code where id < 26) || id = 26

 

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

Switch to our mobile site