Jump to content

mysql select previous 5 rows, and next 5 rows.


shortysbest

Recommended Posts

I have an image slideshow and I want to show the next 5 images from the current displayed image, and the previous 5 images before the displayed image as well as the current image.

 

so to sort of demonstrate:

 

-IMG ID-

192

193

195 (skipped because it may have been deleted)

199

200

201 (current displayed image)

202

204

205

206

211

 

this is the code that gets the first 9 images from the database (showing just to show the variables)

another variable that would be added to make it work how I want would be the current image id, so that could just simply look like: id<$photo_id

$get_photos_query = mysql_query("SELECT * FROM photos WHERE user_id='$id' ORDER BY date DESC LIMIT 9");

 

 

Link to comment
Share on other sites

I've been trying to do it like this though, the problem is making the variables for stop and end since the id of the row isn't always (and almost never will be) in order like 1,2,3,4,5, with no breaks, as i said before they would be like 1,4,7,19,20, 21 etc.

Link to comment
Share on other sites

I've been trying to do it like this though, the problem is making the variables for stop and end since the id of the row isn't always (and almost never will be) in order like 1,2,3,4,5, with no breaks, as i said before they would be like 1,4,7,19,20, 21 etc.

 

It doesn't matter what the ID is, the LIMIT function works the same either way.

Link to comment
Share on other sites

Yes I know, however I need a starting point to add and subtract from in order to get the 5 images before and the 5 images after the current image. Kneed a way to get the true row number, not the unique Id. One way would be to put it in a while loop for every image comparing the IDs but that would be way too much on the server if I were to have say 5 million images in the database... (or eventually billions)

Link to comment
Share on other sites

Here ya go. Keep in mind, this isn't exactly a FAST query.

 

SELECT `col1`,`id` FROM `table` WHERE `id` = 3
UNION ALL (
  SELECT `col1`,`id` FROM `table` WHERE `id` < 3 ORDER BY `id` DESC LIMIT 10
) UNION ALL (
  SELECT `col1`,`id` FROM `table` WHERE `id` > 3 LIMIT 10
) ORDER BY `id`

 

Replace 3 with your $id

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.