www.webdeveloper.com
Results 1 to 3 of 3

Thread: Selecting from either side of an id

  1. #1
    Join Date
    Oct 2010
    Location
    Ohio
    Posts
    233

    Selecting from either side of an id

    Hi guys, I have this basic table
    id_image int unsigned PK
    image_file varchar(50)
    image_ext varchar(5)
    image_upload_date datetime
    gallery_type tinyint unsigned

    What I'm having problems with is selecting 3 images before and 3 images after a given ID. Considering the ids aren't necessarily all inclusive and I may have to narrow the results (IE images from a given month, or of a given type). Any ideas would be greatly appreciated.
    Last edited by Derokorian; 04-30-2011 at 12:37 AM.
    ~Ryan
    www.rdennispallas.com <-- Personal Site, changing regularly cuz its ugly.

  2. #2
    Join Date
    Oct 2010
    Location
    Ohio
    Posts
    233
    Code:
    (SELECT * FROM dev_images WHERE id_image>? ORDER BY image_upload_date ASC LIMIT 3)
       UNION
    (SELECT * FROM dev_images WHERE id_image<? ORDER BY image_upload_date DESC LIMIT 3)
       UNION
    (SELECT * FROM dev_images WHERE id_image=?)
       ORDER BY id_image ASC
    This is what I ended up with, seems to work exactly as wanted (making a filmstrip around the currently views image). Hope this helps someone.
    ~Ryan
    www.rdennispallas.com <-- Personal Site, changing regularly cuz its ugly.

  3. #3
    Join Date
    Jul 2010
    Location
    /ramdisk/
    Posts
    865
    Code:
    SELECT dev_images.* 
    FROM dev_images
    
    WHERE id_image >= (?#?)-3
    AND id_image < (?#?)+3
    
    ORDER BY id_image ASC

    Maybe that's what you wanted? The UNIONS are a bit odd, I would try and avoid them if an alternative exists where you can put the constraints into the WHERE clause.
    I use (, ; : -) as I please- instead of learning the English language specification: I decided to learn Scheme and Java;

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles