mysql查询随机几条数据(速度快)
MySql查询随机几条数据
想到了 Max RAND 这几个函数
用以下2种办法都可以实现查询。 速度还行。
几十万数据左右, 没有什么问题。
SELECT * FROM `news`
WHERE id >= (SELECT floor(RAND() * (SELECT MAX(id) FROM `news`)))
LIMIT 10;
SELECT *
FROM `news` AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(id) FROM `news`)-(SELECT MIN(id) FROM `news`))+(SELECT MIN(id) FROM `news`)) AS id) AS t2
WHERE t1.id >= t2.id
LIMIT 10;