List:General Discussion« Previous MessageNext Message »
From:Christian Mack Date:May 17 1999 10:29am
Subject:Re: random rows
View as plain text  
Xah Lee wrote:
> Suppose I want 10 rows randomly selected from a table. How do I do that?
> Thanks.
> Xah

Hi Xah

1) Add a FLOAT column to the table:
ALTER TABLE random_selectable ADD random FLOAT;

2) Fill this column with random numbers:
UPDATE random_selectable SET random = RAND();

3) Get 10 values:
SELECT * FROM random_selectable ORDER BY random LIMIT 10;

If you don't want to UPDATE the random column for each SELECT, then you can add a random
criteria in the WHERE clause e.g.:
SELECT * FROM random_selectable WHERE random > RAND()/2 ORDER BY random LIMIT 10


random rowsXah Lee15 May
Re: random rowsChristian Mack17 May
Re: random rowsXah Lee18 May
Re: random rowsChristian Mack18 May