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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:50:04+00:00 2026-06-07T07:50:04+00:00

I am trying to obtain a random row from the result of an SQL

  • 0

I am trying to obtain a random row from the result of an SQL query.

My query is as follows

SET @rank = 0;
SELECT * FROM(
     SELECT (@rank:=@rank+1) AS num, ...
     FROM ...
     WHERE ....) as raw
WHERE raw.num = FLOOR(1 + (RAND() * @rank)) LIMIT 1

The general idea being that each row of the table from the inner query result is given a unique number (num). I have manually checked that this is indeed the case and that every row is numbered.

The last line is causing me distress. As it stands, WHERE num = FLOOR(1 + (RAND() * @rank)) LIMIT 1 is returning what I want – only half the time. It appears to be returning random rows within the correct range (which for the example I am testing the query on is 0-1299). However, one in every three queries returns absolutely nothing.

Ok, so I thought maybe it was a double precision issue, so I tried using >= as follows: WHERE num >= FLOOR(1 + (RAND() * @rank)) LIMIT 1. The result in this case is confusing me. With this code I always get a result, but the number of the row returned is always < 100.

So if we call FLOOR(1 + (RAND() * @rank)) x. When I use = rather than >= it confirms that x must (in some cases) be equal to numbers greater than 1000. However, when using >=, the fact that the condition is satisfied means that x must always be less than 100?

What’s going on? or how else can I solve my problem

  • 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-06-07T07:50:05+00:00Added an answer on June 7, 2026 at 7:50 am

    I think the problem is that the RAND() function in your query is being called multiple times, once for each row returned from raw. If that’s what’s happening, then it’s possible that it won’t find any rows that satisfy the predicate, since it is comparing each row to a different target. (Is the first row the fifth row? Is the second row the third row? etc.)

    I would move the call to RAND() and the initial assignment of @rank to the beginning of the query, something like this:

    SELECT * FROM(
         SELECT (@rank:=@rank+1) AS num, ...
         FROM (SELECT @rand := RAND(), @rank := 0) r
         CROSS JOIN ...
         WHERE ....) as raw
    WHERE raw.num = FLOOR(1 + @rand * @rank) LIMIT 1
    

    — or, in keeping with your pattern of using a separate SET statement —

    SET @rand = RAND();
    SET @rank = 0;
    SELECT * FROM(
         SELECT (@rank:=@rank+1) AS num, ...
         FROM ...
         WHERE ....) as raw
    WHERE raw.num = FLOOR(1 + @rand * @rank) LIMIT 1
    

    (I happen to prefer the former, as it runs as a single statement; it’s not dependent on user variables being set outside of the SELECT statement.)

    But either of those should make sure that the call to the RAND() function is happening exactly once (at the beginning of your query).

    Other than that, I don’t have a good explanation as to the behavior you are seeing.

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

Sidebar

Related Questions

I'm trying to obtain a result from a web service in a java program.
In MysQL I am trying to obtain a certain result set, but am unsure
I'm trying to find a MySQL query which will obtain the time that is
I am trying to set a fetch request with a predicate to obtain records
I am trying to obtain the ip address from a text file so when
I'm trying to obtain the n-th elements from a list of tuples. I have
I'm trying to obtain the price from a field called DiscountMarkupPriceRate1 on my second
I am trying to obtain and transfer information from a HTML web form (through
I'm trying to obtain the keywords from an HTML page that I'm scraping with
I am trying to obtain a variable from a different file type and import

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.