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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:19:15+00:00 2026-05-21T21:19:15+00:00

How can I select next and previous rows for a total of six rows

  • 0

How can I select next and previous rows for a total of six rows in a query but with conditions:

I’ll always have 6 rows, so if i have:

1, 2, 3, 4, 5, 6, 7, 8, 9

and my ID is 2 the result must be:

1,3,4,5,6,7

if my ID is 5 the result must be:

2,3,4,6,7,8

if my ID is 9 the result must be:

3,4,5,6,7,8

edit:

(SELECT * FROM articulos 
      WHERE categoria = 'Y'
      AND numero >= 
       (SELECT IF(
          (SELECT COUNT(*) FROM articulos WHERE numero < X AND categoria = 'Y')<3,
          (SELECT MIN(X) FROM articulos WHERE categoria = 'Y'),
          (X-3)
       ))
      ORDER by numero ASC LIMIT 6)
UNION
(SELECT * FROM articulos 
      WHERE categoria = 'Y'
      AND numero <= 
       (SELECT IF(
          (SELECT COUNT(*) FROM articulos WHERE numero > 1605 AND categoria = 'Y')<3,
          (SELECT MAX(X) FROM articulos WHERE categoria = 'Y'),
          (X+3)
       ))
      ORDER by numero DESC LIMIT 6)

maybe this is the solution

  • 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-21T21:19:16+00:00Added an answer on May 21, 2026 at 9:19 pm

    Something like this:

     SELECT data
     FROM table
     JOIN ( SELECT 
        CASE 
           WHEN n > MAX(id)-3 THEN MAX(id) - 7
           WHEN n < 4 THEN 0
           ELSE n - 4 
        END AS lower_bound,
        CASE 
           WHEN n > MAX(id)-3 THEN MAX(id) + 1
           WHEN n < 4 THEN 7
           ELSE n + 4 
        END AS upper_bound
        FROM table ) AS t1
     WHERE id BETWEEN t1.lower_bound AND t1.upper_bound
     AND id <> n
    

    should always select 6 rows

    More info on the CASE function in the docs

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following PHP functions that determine the next and previous rows in
I have a table with many rows so when I make this query: SELECT
I can't figure out how to select a previous/next row IF the current row
How can I select the next node after an defined comment? I know the
As we can select multiple rows into single variable declared as some_table.rowtype in SQL.
I need to write a query that will figure out the previous/next day from
Say I have a table which I query like so: select date, value from
I would like to have a datepicker where user can select only the month
I have 2 buttons next and Previous and I want to move through my
My question deals with my next/previous buttons. I can get my update/delete buttons 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.