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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:18:25+00:00 2026-06-04T12:18:25+00:00

So, interesting problem I’ve run into. I’m sure there’s an easy solution, but I’m

  • 0

So, interesting problem I’ve run into. I’m sure there’s an easy solution, but I’m not sure what it is. 🙂

Basically, imagine a very simple database, like so:

----------------
T1
----------------
  r  |  nID
---------------
  1  |   A
  2  |   B
----------------




----------------
T2
----------------
 nID |  val
---------------
  A  |   XXX
  B  |   L
  B  |   M
  B  |   N
  B  |   P
----------------

Basically, Table 2 references Table 1. Now, I’d like to select a random row from either A or B. However, I would like to first randomize the A and B, THEN choose an associated value.

In other words, flip a coin: Heads, XXX. Tails, L, M, N, or P.

My current query joins the two tables, orders by RAND(), and then LIMIT 1. However, this makes the odds of a B value being chosen much more likely than an A value being chosen. I’m using PHP, so I could easily just run two queries, but running one query would be much tidier, so I want to see what you guys recommend.

Any solutions? =)

EDIT:

Here’s my current query, but it isn’t working. Not sure why!

SELECT *
FROM t2
WHERE
    nID =
    (
        SELECT nID
        FROM t1
        ORDER BY RAND()
        LIMIT 1
    )
ORDER BY RAND()
LIMIT 1

EDIT 2:

To demonstrate the issue I’m having, I created a test case. First, I created the following tables:

T1 and T2

I want the odds of selecting XXX to be identical to selecting L, M, N, or P. The query I have should do it, right? So I tested it. The followings script runs the query 5000 times, and counts the results. They should be about 50-50, with XXX showing up approximately 2500 times, and everything else showing up about 2500 times as well.

$a = 0;
$b = 0;
$i = 0;
while ($i < 5000)
{
    $query = mysql_query("
        SELECT *
        FROM t2
        WHERE
            nID =
            (
                SELECT nID
                FROM t1
                ORDER BY RAND()
                LIMIT 1
            )
        ORDER BY RAND()
        LIMIT 1
        ") or die(mysql_error());

    $result = mysql_fetch_array($query);
    if ($result['val'] == 'XXX')
    {
        $a++;
    }
    else
    {
        $b++;
    }

    $i++;
}

echo "XXX - $a<br />";
echo "Other - $b<br />";

Here are the results:

XXX - 937
Other - 4063

Let’s run it again.

XXX - 968
Other - 4032

And let’s run it one more time.

XXX - 932
Other - 4068

This is hardly the 50-50 split we’d expect to see given my query. What on earth’s going on? Thanks for your help, guys!

  • 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-04T12:18:27+00:00Added an answer on June 4, 2026 at 12:18 pm

    You would expect that the subquery in your question would be run once per outer query, but it looks like that’s not the case. I think the below might give you what you’re after:

    SET @randID = (SELECT nID
    FROM T1
    ORDER BY RAND()
    LIMIT 1);
    
    SELECT VAL
    FROM T2
    WHERE nID = @randID
    ORDER BY RAND()
    LIMIT 1;
    

    (SQL Fiddle)

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

Sidebar

Related Questions

there's this interesting problem i can not solve myself. I will be very glad,
I have an interesting problem, that i am sure has a simple answer, but
I've run into an interesting problem here in using heroku + git + SVN.
i have a very interesting problem. background i've used TTreeView for years but only
I run into an interesting problem I want to understand. I have a table:
I ran into an interesting problem at work today. I got a request to
We had a very interesting problem with a Win Forms project. It's been resolved.
Here's an interesting problem. Is there a way to write some code using LINQ
i have an interesting problem in my delphi 2009 app. when run in the
I encountered an interesting problem today which I thought was not possible in Java.

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.