Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 708737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:25:28+00:00 2026-05-14T04:25:28+00:00

Mysql: i need to get the offset of a item in a query. I

  • 0

Mysql: i need to get the offset of a item in a query.

I have a image gallery: this show 6 image per stack, so when i request image 22 it shows images from 18 to 24. It should first get the offset of the image 22, then get the images from 18 to 24.

Another example: i request the image number 62(and offset 62), it will select images with offset from 60 to 66.

Is possible with a single query?

The main important thing is to get the offset value of the item that has its id equal to a number.

Thanks 😉

EDIT:
select * from images order_by updated_at offset(here i need to get the offset of the image id in this query, and the make some calculation… this is what i need, if is possible.. :d)

EDIT2:
Now I understand that I need 2 queries:

1º: get the offset of the image within the query with my custom order

2º: get the images using the offset from the first query… this I can make it alone, the first one is the problem.. :s

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:25:28+00:00Added an answer on May 14, 2026 at 4:25 am

    If your images have sequential IDs, you may want to do the following:

    SELECT    * 
    FROM      images 
    WHERE     id >= ((? DIV 6) * 6) AND 
              id < (((? DIV 6) + 1) * 6)
    ORDER BY  id;
    

    Replace the ? parameter in the above query with the ID of the image requested.


    UPDATE: It seems that your images are not ordered by a sequential ID, but by a timestamp. Unfortunately it looks like MySQL does not support variable expressions in the LIMIT clause (Source). One option would be to use a prepared statement:

    PREPARE stmt FROM 
    " SELECT    * 
      FROM      images
      ORDER BY  updated_at
      LIMIT     ?, 6";
    
    SET @lower_limit := ((22 DIV 6) * 6);
    
    EXECUTE stmt USING @lower_limit;
    

    Another option could be:

    SET @row = 0;
    
    SELECT    * 
    FROM      images 
    WHERE     (@row := @row + 1) BETWEEN ((3 DIV 6) * 6) + 1 and (((3 DIV 6) + 1) * 6)
    ORDER BY  updated_at;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mySQL query to get columns from a table like this: String
I have a mysql query. I need to get last value from columns Lat,Lng
I'm using MySQL 5, and I need to do this sentence to get the
I have two mysql tables, courses and student_courses, I need to get unique course_names
I have a big MySQL users table and need to get six random rows
I have a mysql table and I need to get random row and get
I have result in MySQL table. I need to get result only where is
I need to get the IDs around an ID with mySQL. Example: IDs ​​in
I need to search a string in mysql with php. I get an error
I am newbee with PHP and MySQL and need help... I have two tables

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.