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 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

Ask A Question

Stats

  • Questions 68k
  • Answers 68k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Late response but you could just keep it simple and… May 11, 2026 at 12:13 pm
  • added an answer test_variable should be global in test.py. You're getting a name… May 11, 2026 at 12:13 pm
  • added an answer Use the DOMDocument::save() method to save the XML document into… May 11, 2026 at 12:13 pm

Related Questions

I need to display a variable-length message and allow the text to be selectable.
I need to display a string which has a white space on a asp.net
I'm working on a Cocoa Mac app where I need to display a window/view
I have a need to display a UITableView containing a user's account credentials. For
I need to display the log entries from a database. Of course the log
I need to display an error message on rejecting a drop in my application.
I need to display many pages of news in a site. Should I do
I need to display additional information, like a tooltip, but it's a lot of
I need to display text in an iPhone application, something like the way a
I need to display something such as below. Type A Type A Item 1

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.