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 5844347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:16:47+00:00 2026-05-22T12:16:47+00:00

My challenge is that I have a table of e.g. posts in a forum

  • 0

My challenge is that I have a table of e.g. posts in a forum and I want to get the offset for LIMIT clause right.

To illustrate the example:
Table Post – please remember OFFSET is the number/index of the row.

OFFSET 0: ID = 17, AUTHOR = ABC, CREATE_DATE = 2011-05-01
OFFSET 1: ID = 39, AUTHOR = ABC, CREATE_DATE = 2011-05-02
OFFSET 2: ID = 77, AUTHOR = ABC, CREATE_DATE = 2011-05-03
OFFSET 3: ID = 78, AUTHOR = ABC, CREATE_DATE = 2011-05-04
OFFSET 4: ID = 99, AUTHOR = ABC, CREATE_DATE = 2011-05-05
OFFSET 5: ID = 33, AUTHOR = ABC, CREATE_DATE = 2011-05-06
OFFSET 6: ID = 45, AUTHOR = ABC, CREATE_DATE = 2011-05-07
OFFSET 7: ID = 11, AUTHOR = ABC, CREATE_DATE = 2011-05-08
OFFSET 8: ID = 13, AUTHOR = ABC, CREATE_DATE = 2011-05-09
OFFSET 9: ID = 88, AUTHOR = ABC, CREATE_DATE = 2011-05-10

Now I want to make query for ID = 45, and my pagination should give a result of 5 entries per page.

SELECT Table.* FROM Table WHERE Table.id = 45 ORDER BY Table.create_date;

Result is one row with ID = 45. And then I want to know that the offset is 6 in relation to the 10 entries in total in the table. I want 5 rows per page, so x = 5 for the following statement.

In other words I know the the row with offset = 6 is on the 2nd page of the results page with 5 entries per page.

SELECT Table.* FROM Table LIMIT x, 5

How do I know x?

My first idea is that I fetch all IDs from the table and just look for the right ID and then divide the offset by 5.

Question: Is there a better way to do this in SQL because to me this seems like a very common challenge?

Bonus question: SQL solution is fine; how to do it in MySQL specifically if there is a difference?

  • 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-22T12:16:47+00:00Added an answer on May 22, 2026 at 12:16 pm

    You’re usually better off wrapping the whole thing in a transaction with multiple queries. Pseudocode:

    select count(*) from table
    where create_date <= (select create_date from table where id = 45);
    
    select table.* from table order by create_date limit 5 offset :offset;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a website for a 'walking challenge', which has a table that
I have a challenge that I am trying to solve and I can't work
Challenge: I have this code that fails to compile. Can you figure out what's
I have been set a challenge to create an indexer that takes all words
I have a bit of a unique challenge today. I have a client that
I have a table with day/time values that are split into date and an
A challenge for the excel users: I have a table with the following columns:
I have a table inside a form that looks as follows: <td><input type=text name=code[0]
All right, so here's a challenge for all you SQL pros: I have a
I have a similar challenge to this post: Batch insert/update with entity framework from

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.