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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:21:39+00:00 2026-05-11T06:21:39+00:00

I need to display a list of records from a database table ordered by

  • 0

I need to display a list of records from a database table ordered by some numeric column. The table looks like this:

CREATE TABLE  items (   position int NOT NULL,   name varchar(100) NOT NULL, ); INSERT INTO items (position, name) VALUE (1, 'first'), (5, 'second'), (8, 'third'), (9, 'fourth'), (15, 'fifth'), (20, 'sixth'); 

Now, the order of the list should change according to a parameter provided by the user. This parameter specifies which record comes first like this:

position = 0 order should be = 1, 5, 8, 9, 15, 20  position = 1 order should be = 20, 1, 5, 8, 9, 15  position = 2 order should be = 15, 20, 1, 5, 8, 9 

In other words the last record becomes the first and so on. Can you think of a way to do this in SQL?

I’m using MySQL but an example in any SQL database will do.

Thanks

  • 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. 2026-05-11T06:21:40+00:00Added an answer on May 11, 2026 at 6:21 am

    See how this works for you. Uses generic SQL so it should be valid for MySql (untested) as well.

    DECLARE @user_sort INTEGER SET @user_sort = 0  SELECT position, name FROM  (   SELECT I1.position, I1.name, COUNT(*) AS rownumber, (SELECT COUNT(*) FROM items) AS maxrows   FROM items I1, items I2   WHERE I2.position <= I1.position   GROUP BY I1.position, I1.name ) Q1 ORDER BY    CASE WHEN maxrows - rownumber < (@user_sort % maxrows) THEN 1 ELSE 2 END, position 

    Note: * If the user provided sort index is greater than the row count, the value will wrap to within the valid range. To remove this functionality, remove the ‘% maxrows’ from the ORDER BY.

    Results:

    SET @user_sort = 0

    position    name 1   first 5   second 8   third 9   fourth 15  fifth 20  sixth 

    SET @user_sort = 1

    position    name 20  sixth 1   first 5   second 8   third 9   fourth 15  fifth 

    SET @user_sort = 2

    position    name 15  fifth 20  sixth 1   first 5   second 8   third 9   fourth 

    SET @user_sort = 9

    9   fourth 15  fifth 20  sixth 1   first 5   second 8   third 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to display a list of dates from a database. There can be
I need to display a list of dates, which I have in a table
I need to display the list of names which I am getting from the
I need to display a single list, ordered by date which contains different types
I need to display content from an OLAP cube in Sharepoint. This could be
I need to fetch images and some other data from server and then display
I have system in which I need to display the list of records such
I have a gridview and binding the records in it from a list.I need
Here is the case: I need to display a list of records on a
I need to display a real time data from MS SQL 2005. I saw

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.