Select a record randomly in MySQL with PHP

-3

How can I randomly select a record in a database that meets a condition (that is, it is also possible to use the WHILE).

mysql_query("SELECT * FROM mitabla WHERE cat=4 ORDER BY RAND() LIMIT 1");

That example would be fine?

    
asked by ByBrayanYT - Tops y más 14.11.2018 в 17:53
source

1 answer

0

a rand from 1 could be applied to the maximum id value of all records

example:

mysql_query("SELECT * FROM mitabla WHERE cat=4 and id=FLOOR(RAND()*((SELECT max(id) FROM mitabla WHERE cat=4)-1+1)+1) LIMIT 1");
    
answered by 14.11.2018 в 18:07