List:General Discussion« Previous MessageNext Message »
From:Martin Ramsch Date:September 20 1999 5:13pm
Subject:Re: Mysql: Choosing a random record
View as plain text  
On Mon, 1999-09-20 10:02:21 -0500, Joel Chippindale wrote:
> I would like to choose a record at random from a MySQL database.
> I thought that maybe I could order the results of a select using a
> RAND column so that a random record is returned first but the MySQL
> manual specifically says that this will not work.

I work-around for versions of MySQL before V3.23 is:

  SELECT *, id*0+RAND() AS r FROM table ORDER BY r LIMIT 5;

where "id" is some field of your table.

Monty wrote:
| The problem is that without the 'id' MySQL 3.22 thinks that 'r' is
| an constant and will automaticly remove it from the ORDER BY clause.

Martin Ramsch <m.ramsch@stripped> <URL: >
PGP KeyID=0xE8EF4F75 FiPr=52 44 5E F3 B0 B1 38 26  E4 EC 80 58 7B 31 3A D7
Mysql: Choosing a random recordJoel Chippindale20 Sep
  • Re: Mysql: Choosing a random recordMartin Ramsch20 Sep