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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:30:41+00:00 2026-05-30T22:30:41+00:00

I have a problem with order by . We know that if we give

  • 0

I have a problem with order by. We know that if we give integer in order by then it orders via index of column. When I use it this way every time I execute query, I get same result, which is correct.

But when I make this value the result of an expression (I mean I use a expression which always evaluates to 1), then I get 3 possible combinations of result when I execute it.

So for same value which is static, same result; but for same value which is an evaluated expression, different results.

Below is the example of query which I am referring to:

SELECT SomeColumns, (rand()* 10) as rand
FROM TableName
ORDER BY (if ((rand in (1) ), 1,1))
LIMIT 0 , 6

The above query gives three possible result sets.

  • 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-30T22:30:43+00:00Added an answer on May 30, 2026 at 10:30 pm

    ORDER BY‘s syntax is:

    ORDER BY {col_name | expr | position} [ASC | DESC], ...
    

    Although the following would appear to the human-eye to be equivalent:

    ORDER BY 1
    ORDER BY (if ((rand in (1) ), 1,1))
    

    they are not.

    • The first one sorts by the 1st column’s data, as it’s an integer with value 1.

    • The second one, although it evaluates to 1, is not an integer but an expression and, as such, a different sorting mechanism is invoked. For each row, the expression 1 is evaluated and used to determine the row’s position; because your expression actually doesn’t refer to row data at all, your results will be completely unpredictable.

      Whether you’ll get the table’s “natural order” (that is, the physical ordering that happens to be present underneath the relational abstractions) or some random-looking order that the ORDER BY created I couldn’t say, but it also doesn’t matter.

    Usually you’d write something like this for an expression:

    ORDER BY `col` % 6
    

    … i.e. something that references data.

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

Sidebar

Related Questions

I have some problem to order an array by a field of this, here
I have problem when I try insert some data to Informix TEXT column via
I have a strange problem, I am trying to order the output of a
I have problem in some JavaScript that I am writing where the Switch statement
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
So I'm new working with paypal, the problem I have is that I don't
Let's say that I have a boost::variant<std::string, int> myVariant; In this object I keep
I have a problem, e am executing a R script from php via command
I have some inherited JS code that uses this format: function main(param) { var
Can someone find a strategy for this problem that DOESN'T INVOLVE CONVERTING TO BASE

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.