Re: select rand() returning unexpected results Walter G. wrote:
> Okay, that makes sense. Of course the LIMIT won't work the way I need
> it, since often I get no results (not to mention the performance hit
> I'm taking of having to create 20K rands every time I do a simple
> search). Oh well.
>
> Thanks,
>
> Walter
>
> toby wrote:
>
> > 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;
On my 5.0.28 this *always* returns a single row, which is what you
want? (But I think ORDER BY RAND() may not work as expected in some
older versions. What version are you using?)
For speed, have you tried:
SELECT COUNT(*) FROM nouns INTO @cnt; -- or keep external count
SELECT FLOOR(RAND()*@cnt) INTO @id; -- or generate externally
SELECT word FROM nouns WHERE id=@id; |