Walter G. wrote:
> Hi,
>
> I've written a small program to return a random word in a database
> dependent on an id. The ids in the table are 0..n, consecutive and
> non-repeating (I've double-checked). To implement this, I tried
>
> SELECT word FROM nouns WHERE id=FLOOR(RAND()*{table row count});
> ...
> The problem is that executing the statement often returns an unexpected
> number of records. ...
I think the short answer is that the WHERE expression, hence RAND(), is
being independently evaluated for each row.
Try:
SELECT word FROM nouns ORDER BY RAND() LIMIT 1;
>
> Thanks,
>
> Walter Gildersleeve
> Freiburg, Germany
>
> P.S. Here's the table definition:
>
> word varchar(255)
> id smallint(6) PRI 0 (indexed)
>
> The table has 27,166 records. I tried this on "Ver 14.7 Distrib
> 4.1.16, for Win32 (ia32)" and "Ver 14.7 Distrib 4.1.20, for
> redhat-linux-gnu (i686) using readline 4.3".
>
> Here's a page that demonstrates the problem:
>
> http://devhed.com/perform_query.plx
>
> ----------------------------
> To e-mail me directly, simply reverse the characters in the e-mail left
> of the at sign.