Choosing a random result from MySQL

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

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

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

Is there something that I am not adding or doing right here?

Thank you for your attention.

+6
source share
5 answers

The name of the function you are looking for is RAND () .

+14
source

You need ORDER BY RAND()

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

MySQL uses RAND () instead of RANDOM ().

+2
source

Since this is MySQL, you need to use RAND () instead of RANDOM ().

+1
source

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

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

You can check it out.

+1
source

Source: https://habr.com/ru/post/894551/


All Articles