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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:52:40+00:00 2026-05-27T06:52:40+00:00

Is it possible to sort in MySQL by "order by" using a predefined set

  • 0

Is it possible to sort in MySQL by "order by" using a predefined set of column values (ID) like order by (ID=1,5,4,3) so I would get records 1, 5, 4, 3 in that order out?

UPDATE: Why I need this…

I want my records to change sort randomly every 5 minutes. I have a cron task to update the table to put different, random sort order in it.

There is just one problem! PAGINATION.

I will have visitors who come to my page, and I will give them the first 20 results. They will wait 6 minutes, go to page 2 and have the wrong results as the sort order has already changed.

So I thought that if I put all the IDs into a session on page 2, we get the correct records even if the sorting had already changed.

Is there any other better way to do this?

  • 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-27T06:52:41+00:00Added an answer on May 27, 2026 at 6:52 am

    You can use ORDER BY and FIELD function.
    See http://lists.mysql.com/mysql/209784

    SELECT * FROM table ORDER BY FIELD(ID,1,5,4,3)
    

    It uses Field() function, Which “Returns the index (position) of str in the str1, str2, str3, … list. Returns 0 if str is not found” according to the documentation. So actually you sort the result set by the return value of this function which is the index of the field value in the given set.

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

Sidebar

Related Questions

I know that JTable can sort by a single column. But is it possible
Possible Duplicate: Default sort-ordering in MySQL (ALTER TABLE … ORDER BY …;) I have
I'm looking for a way to order/sort a set of strings that define software
I would like to include a feature that lets users of my site sort
Is it possible to sort the values retrieved from MySQL, in say descending id
Is it possible to sort a TreeView collection using IComparer<T> and not IComparer ?
I'd like to use the somelist.sort() method to do this if possible. I have
Is it possible to get the height of a 'text' field from mysql? Or
Is it possible to order alphabetically in mySQL in the following fashion: A1 A2
I would like to sort the rows in a table based on an arbitrary

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.