Mysql method for obtaining random data bitsCN.com
1. order by rand ()
The data is too slow and the randomness is very good. it is suitable for situations with very small data volumes.
SELECT * FROM table_name AS r1 JOIN (SELECT (ROUND (RAND () * (select abs (MAX (id)-MIN (id)-$ limit) FROM table_name )) + (select min (id) from table_name) AS id) AS r2 WHERE r1.id> = r2.id order by r1.id asc limit $ limit;
The speed is very fast, but the obtained data is sequential with IDs, which cannot be solved.
The subquery generates a random number.
Rand () is a random number ranging from 0 to 1, which can be 0 to 1.
MAX (id)-MIN (id) also needs to subtract $ limit to avoid the number of results being less than $ limit
Because the primary table id may be discontinuous, r1.id must be> = r2.id
Note that sometimes the minimum id does not start from 1 and may start from a large number. Therefore, the M (id) value must be added.
Manual content: to obtain a random integer R in the range of I ≤ R ≤ j, the expression FLOOR (I + RAND () * (j