SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Evangelist
    Join Date
    Dec 2004
    Posts
    419
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Query about how to do a simple(ish) Select

    I have photo details stored in table j_p1

    If I am displaying photoID 732, for example, I would like to use SQL to output 5 photos near the current photo - e.g, I'd like to display photos

    730
    731
    >> 732 (Current photo)
    733
    734

    So it finds photos two either side of the current photo.

    My babyish SQL can do this:
    Code:
    SELECT photoID, photoFileName, photoFileType 
    FROM j_p1 
    WHERE photoID < 732 
    ORDER BY photoID DESC LIMIT 5
    But I'm not sure how to arrange things so that it returns 5, with the current photo selected in the middle of the result set.

    Any advice much appreciated.

    Thanks

  2. #2
    SitePoint Addict Phidev's Avatar
    Join Date
    Oct 2008
    Location
    Texas
    Posts
    204
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My quick fix would be something like

    Code MySQL:
    SELECT photoID, photoFileName, photoFileType 
    FROM j_p1 
    WHERE photoID BETWEEN (732 - 2) AND (732 + 2) 
    ORDER BY photoID DESC LIMIT 5


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •