Re: random rows using MySQL's rand()
| From: | Tom Rogers | Date: | Tue, 22 Oct 2002 02:53:10 +0000 |
| Subject: | Re: random rows using MySQL's rand() | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-120847@lists.php.net to get a copy of this message | ||
Hi,
Tuesday, October 22, 2002, 10:54:05 AM, you wrote:
T> Hi all,
T> I was trying to use the following query to randomly retrieve a row from
T> MySQL table.
T> $query = "SELECT * FROM my_table ORDER BY RAND() LIMIT 1";
T> This query works fine in MySQL client, access via command-line.
T> But when using this via PHP, it doesn't retrieve the row randomly for a
T> small set of records, ie. 3.
T> Now that I've increased the number of records to 32, it starts to show
T> random behaviour.
T> So, just wondering whether this is something I can expect for a table with
T> only a few records.
T> Regards,
T> Tanat
For small numbers you would probably be better off generating the
random number in php from 1 to numrows and use that in the select like:
LIMIT $rand,1
--
regards,
Tom