简体   繁体   中英

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.

您正在寻找的函数名称是RAND()

你需要ORDER BY RAND()

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

MySQL使用RAND()而不是RANDOM()。

因为它是MySQL,所以你需要使用RAND()而不是RANDOM()。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM