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

  • SEARCH
  • Home
  • 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 7502101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:45:10+00:00 2026-05-29T20:45:10+00:00

I want to be able to target one record, and get back that record,

  • 0

I want to be able to target one record, and get back that record, and a variable amount of records on each side of it.

Like say you have a key id of 2356. So you need 2356 and the 3 records before and after 2356, with an order by on create_ts (for example).

I am wondering if there is a way to do this in one query (mysql)?

  • 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-29T20:45:11+00:00Added an answer on May 29, 2026 at 8:45 pm

    The table (call it mytable) has to have a compound index on create_ts and id

    ALTER TABLE mytable ADD INDEX create_ts_id_index (create_ts,id);
    

    You evidently need two queries to pick up needed keys: 3 before id, the id itself, 3 after id.

    That’s 7 ids.

    This query picks up 4 keys (id + 3 after)

    SELECT create_ts,id FROM mytable WHERE id >= 2356 ORDER BY create_ts LIMIT 4;
    

    This query picks up 4 keys (id + 3 before)

    SELECT create_ts,id FROM mytable WHERE id < 2356 ORDER BY create_ts DESC LIMIT 4;
    

    A UNION of the two queries should eliminate a duplicate of id 2356

    Let’s combine these and perform an INNER JOIN of it to mytable

    SELECT B.* FROM
    (
        SELECT * FROM
        (SELECT create_ts,id FROM mytable
        WHERE id >= 2356 ORDER BY create_ts
        LIMIT 4) AA
        UNION
        (SELECT create_ts,id FROM mytable
        WHERE id <= 2356 ORDER BY create_ts DESC
        LIMIT 4)
    ) A
    INNER JOIN mytable B USING (create_ts,id)
    ORDER BY A.create_ts,A.id;
    

    The subquery A should only have 7 keys. Once those 7 keys are retrieved, the INNER JOIN should be quick.

    Whenever you need N keys before and N keys after for this query, just use LIMIT N+1.

    I would suggest this method because we can neither assume that 3 keys back is id-3, nor assume 3 keys after is id+3. This is especially true if the table has gaps in the ids for any reason.

    Give it a Try !!!

    CAVEAT : I did not try it out. This is what you want algorithmically. The MySQL syntax may or may not allow for it. It the syntax is not right, I’ll try to construct an example and fix the syntax.

    Just in case, here is a more stable approach.

    CREATE TABLE create_ts_ids SELECT create_ts,id FROM mytable WHERE 1=2;
    ALTER TABLE create_ts_ids ADD PRIMARY KEY (id);
    INSERT INTO create_ts_ids
    SELECT create_ts,id FROM mytable
    WHERE id >= 2356 ORDER BY create_ts LIMIT 4;
    INSERT IGNORE INTO create_ts_ids
    SELECT create_ts,id FROM mytable
    WHERE id <= 2356 ORDER BY create_ts DESC LIMIT 4;
    SELECT B.* FROM create_ts_ids  A
    INNER JOIN mytable B USING (create_ts,id)
    ORDER BY A.create_ts,A.id;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to capture the exception that is thrown when a
I want to be able to do: For Each thing In things End For
I want to be able to get an estimate of how much code &
I want to provide 'install' target for Makefile for web application. I'd like to
I have an app that I want to be able to build two different
I'd like to be able to target text links in CSS with border-bottom on
I want to create one MSBuild that will execute two others.. How can i
I have a inline list of pictures that I want to be able to
I want to make a one shot animation but be able to play it
I'm using LaTeX and BibTeX for an article, and I want to able to

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.