之前很多次遇到类似问题,每次都得去搜,还是整理一下吧。


主要解决,随机获取n条数据库中记录。


SELECT *
FROM `table` AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(uid) FROM `table`)-(SELECT MIN(uid) FROM `table`))+(SELECT MIN(uid) FROM `table`)) AS uid) AS t2
WHERE t1.uid >= t2.uid
ORDER BY t1.uid LIMIT 30


总数据量是36,493的情况下,执行时间为:0.0017