Re: random record mysql
| From: | Seairth Jacobs | Date: | Fri, 25 Apr 2003 00:56:25 +0000 |
| Subject: | Re: random record mysql | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-145212@lists.php.net to get a copy of this message | ||
According to New Riders "MySQL" (by Paul DuBois):
If your MySQL is 2.23.3 or later, you could just use ORDER BY RAND() in your
select clause, then get the first record. Prior to that, you would need to
do something like:
SELECT ..., age*0+RAND() as rand_col FROM ... ORDER BY rand_col
Appearently, the "age*0+" bit keeps the optimizer from treating the RAND()
as generating a constant value (and therefore optimizing the ORDER BY clause
out of the query).
If you want to do the random search *after* the query and your php is older
than 4.2.0, you need to make sure you seed php's random number generator
with srand() or mt_srand() before using rand() or mt_rand().
---
Seairth Jacobs
seairth@seairth.com
"Tbo" <tim@copiousdg.com> wrote in message
news:20030425004145.20649.qmail@pb1.pair.com...
> For the life of me I cannot figure out how to get a random record to be
> displayed off a record set using mysql and php. I have seen examples of
the
> rand() function but it only brings up the first record in the record set.
> Any ideas or tutorials? Please help!!!! :)