Re: Randomly select 5 records from a db

From: Date: Tue, 03 Oct 2000 00:27:03 +0000
Subject: Re: Randomly select 5 records from a db
References: 1 2 3 4 5 6 7  Groups: php.general 
Request: Send a blank email to php-general+get-18333@lists.php.net to get a copy of this message
At 7:22 PM -0500 10-02-2000, Ben Weinberger wrote:
Yes, you're doing something wrong. :-) Let's see the exact query. Copy and paste it here from your script. Maybe you forgot the order by clause.
Here it is... <? // connect to database here $sql = "SELECT *, 3*0+RAND() AS rand_col FROM meminfo WHERE type='bene' ORDER BY rand_col LIMIT 2"; $result= mysql_query($sql); while($myrow=mysql_fetch_array($result)) { print "$myrow[title]<BR>"; } mysql_close($db); ?>
MySQL is optimizing 3*0+RAND() as a constant expression and evaluating it only once. This is a bug. That's why I said you should use 'x', where 'x' is some numeric column in your table. Actually, it might work with any column. But you need to say x*0+RAND(), not 3*0+RAND(). -- Paul DuBois, paul@snake.net

« previous php.general (#18333) next »