MySql查询随机几条数据_MySQL
程序员文章站
2022-05-30 20:22:29
...
bitsCN.com
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;
来源 http://blog.csdn.net/yjflinchong/article/details/8444417