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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:59:42+00:00 2026-05-20T18:59:42+00:00

Can anyone explain what is happening with the following SQL? I understand order by

  • 0

Can anyone explain what is happening with the following SQL? I understand order by rand is not a good practice, but I am interested in an explanation of why this is happening

create database test_views_rand;
use test_views_rand;
create table test_table (number int);

insert into test_table  values (1), (2), (3), (4);

select @test := number from test_table order by rand() limit 1;
# selects a random number from 1 - 4
select @test;
# Always gives 4, regardless of what the actual number was in the select query

Just in case it isn’t clear, the first select statement is random as expected. The second select statement always gives 4, regardless of the outcome of the first select statement.

  • 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-20T18:59:43+00:00Added an answer on May 20, 2026 at 6:59 pm

    The assignments are done before order by like @bw_üezi said.

    select @test := number, @aa:=(@a:=@a+1), @a, @r 
      from test_table, (select @a:=0) a 
     order by (@r:=rand());
    

    results

    || 3 || 3 || 3 || 0.160410950109745  ||
    || 4 || 4 || 4 || 0.0159870376959322 ||
    || 1 || 1 || 1 || 0.161011773311544  ||
    || 2 || 2 || 2 || 0.928689247862146  ||
    

    and then select @test,@aa,@a,@r results

    || 2 || 2 || 4 || 0.015987037695932  ||
    

    How @aa can ever be 4 if in the end it is only 2?

    This is because of the bugfix: http://bugs.mysql.com/bug.php?id=16861

    updates variable from the result_field value when being sent to a client.

    User-variable assignments that are not part of the expression in the select part are done twice (at least for last row, not sure about others)

    Manipulating user-variables inside query can often yield in unexpected results.

    You are better off using query:

    select @test := (select number from test_table order by rand() limit 1);
    

    or

    set @test := (select number from test_table order by rand() limit 1); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone explain why the following is happening: When we serialize a file in
Can anyone explain why following code won't compile? At least on g++ 4.2.4. And
Can anyone explain the following behaviour to a relative newbie... const char cInputFilenameAndPath[] =
Can anyone explain what this mod_rewrite rule is doing? I'm trying to comment the
Can anyone explain what advantages there are to using a tool like MSBuild (or
Can anyone explain in simple words what First and Second Level caching in Hibernate/NHibernate
Can anyone explain the difference between Server.MapPath(.) , Server.MapPath(~) , Server.MapPath(@\) and Server.MapPath(/) ?
Can anyone explain the meaning of and purposes of the values for the SecurityAction
Can anyone explain to me what this means? Run-Time Check Failure #0 - The
Can anyone explain the differences between Protocols and Categories in Objective-C? When do you

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.