Alan
Alan

Reputation: 471

Selecting Random Result from MySQL

I wanted to randomly select results from mysql database with this code:

$data = mysql_query("SELECT * FROM people ORDER BY RANDOM() LIMIT 4") or die(mysql_error()); 

I got an error message: FUNCTION members.RANDOM does not exist

Is there something I'm not adding or doing right here?

Thanks for your asistance.

Upvotes: 7

Views: 5639

Answers (5)

Casey Flynn
Casey Flynn

Reputation: 14048

This is a very good source of advice for efficiently retrieving random records from a table with MySQL.

http://www.dasprids.de/blog/2008/06/07/fetching-random-rows-of-mysql-efficiently

You might want to check it out.

Upvotes: 1

Jerry Brady
Jerry Brady

Reputation: 3079

Since it's MySQL, you need to use RAND() instead of RANDOM().

Upvotes: 1

Kyle
Kyle

Reputation: 4449

MySQL uses RAND() instead of RANDOM().

Upvotes: 2

Michael Berkowski
Michael Berkowski

Reputation: 270775

You need ORDER BY RAND()

$data = mysql_query("SELECT * FROM people ORDER BY RAND() LIMIT 4") or die(mysql_error());

Upvotes: 8

JK.
JK.

Reputation: 5136

The function name you're looking for is RAND().

Upvotes: 15

Related Questions