List:General Discussion« Previous MessageNext Message »
From:Michael Widenius Date:September 17 1999 11:57pm
Subject:Re: SELECT Random sets possible?
View as plain text  
>>>>> "Christian" == Christian Mack <Mack@stripped> writes:

Christian> David Fallon wrote:
>> 
>> Is it possible to SELECT a random recordset out of a database? If so, what's
>> the syntax? (I want to get a random selection of 5 items from a table).
>> 
>> I'm not subscribed to the list, so please reply directly to me.

Christian> Hi David

Christian> I've not tested this myself.
Christian> You have to use 3.23.3 for this to work (which I currently don't use):
Christian> SELECT * FROM table ORDER BY RND() LIMIT 5;

Christian> Or you can add a column and fill it randomly to sort after this (works with
> 3.22.xx versions):
Christian> ALTER TABLE table ADD random DOUBLE;
Christian> UPDATE table SET random=RND();
Christian> SELECT * FROM table ORDER BY random LIMIT 5;

Another option is to do the following:

SELECT *,rand() as r from table ORDER BY r LIMIT 5

In this case you must just ignore the last column in your application.

Regards,
Monty
Thread
SELECT Random sets possible?David Fallon16 Sep
  • Re: SELECT Random sets possible?Christian Mack17 Sep
    • Re: SELECT Random sets possible?Michael Widenius18 Sep
      • Re: SELECT Random sets possible?Martin Ramsch18 Sep
        • Re: SELECT Random sets possible?Michael Widenius19 Sep