SELECT * FROM `table`
WHERE id >= (SELECT floor( RAND() * ((SELECT MAX(id) FROM `table`)-(SELECT MIN(id) FROM `table`)) + (SELECT MIN(id) FROM `table`)))
ORDER BY id LIMIT 1;
SELECT *
FROM `table` AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(id) FROM `table`)-(SELECT MIN(id) FROM `table`))+(SELECT MIN(id) FROM `table`)) AS id) AS t2
WHERE t1.id >= t2.id
ORDER BY t1.id LIMIT 1;
发布评论
评论(2)
1、随机跳到指定的行,
这样会产生连续的5条记录。解决办法只能是每次查询一条,查询5次。
2、
这种方法效率低于join查询;
上面这两种方法会一直纠结在查询前面的部分数据,加上min之后就可以全表随机查找了,这是最后的sql语句
随机跳到表指定行,顺着取固定数量的用户,这样应该高效。