There is a query like:

$query = mysql_query("SELECT name FROM my_table JOIN ( SELECT CEIL(RAND() * ( SELECT MAX(id) FROM my_table )) AS randomID ) AS random_table ON random_table.randomID= my_table.ID "); 

He selects only one record (as it should be random), and LIMIT 10 I need, does not work anywhere, wherever I insert it into this query. Request the wrong kind of what?

    2 answers 2

    Sorry, but if you work with only one table, then why complicate things like that? It can make it easier if your id key is unique:

     SELECT `name` FROM `my_table` WHERE `entry_id` >= ( SELECT FLOOR( MAX(`entry_id`) * RAND() ) FROM `my_table` ) ORDER BY `entry_id` LIMIT 10; 
    • But interesting. That kind of request I dug on the Internet, rereading more than one article. Loved it. It (request) is very smart, correct, etc ... And how to measure the speed of the request? I want to experiment. - Garik Pokrovskij
    • For example, in phpMyAdmin shows the time of the request. Just do it a couple of times to get some average value. - Deonis
     $query = mysql_query("SELECT first(name) FROM my_table JOIN ( SELECT CEIL(RAND() * ( SELECT MAX(id) FROM my_table )) AS randomID ) AS random_table ON random_table.randomID= my_table.ID "); $query = mysql_query("SELECT name FROM my_table JOIN ( SELECT CEIL(RAND() * ( SELECT MAX(id) FROM my_table )) AS randomID ) AS random_table ON random_table.randomID= my_table.ID ") ORDER BY name LIMIT 1; 
    • Parse error: syntax error, unexpected T_STRING in ... ORDER BY name LIMIT 1 is outside of mysql_query (); - Garik Pokrovskij