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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:13:14+00:00 2026-06-10T09:13:14+00:00

I have some query on a table which can be sorted by any of

  • 0

I have some query on a table which can be sorted by any of several columns, and an id of a row in the resultset. How can I get the next or previous records (or the offset of the id in the complete resultset).

ie.
let’s say I have the following table

ID First_Col Second_Col Name
1   34        24         John
2   40        22         James
3   48        5          Hugh

If sorting by first_col, and given id 2, the next record would be 3, but if sorting by Second_Col, the next record would be 1.

Given the sortBy expression (which is passed to my function), and the id, how can I find the next record easily?

  • 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-06-10T09:13:15+00:00Added an answer on June 10, 2026 at 9:13 am

    This should work:

    SELECT
    *
    FROM
    names
    WHERE
    Second_Col > (SELECT Second_Col FROM names WHERE ID = 2)
    ORDER BY
    Second_Col 
    LIMIT 1
    

    See http://www.sqlfiddle.com/#!9/a84e9c/1

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

Sidebar

Related Questions

Given I have table with some data... I have a query which is used
I have the query to select some columns from two tables, which must contain
I've written some code to add a table row which you can see below.
I have a table which looks something like the following (first row is columns):
I have some query. First of all let's have an iPhone application flow which
I have a query which can be expressed 2 different ways with the same
I have a query which retrieves the data from Oracle DB. The table which
I have a column called CODE in a MySQL table which can be NULL.
I have some option-item combination in my application from which the user can choose
I have a table which has columns _id = Primary Key Auto Increment Title

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.