从数据库随机取出数据

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jzlcheng/article/details/79310912

SELECT description FROM question ORDER BY RAND() LIMIT 1;

在ORDER BY从句里面不能使用RAND()函数,因为这样会导致数据列被多次扫描。

SELECT description FROM question WHERE id >= (SELECT FLOOR(RAND() * (SELECT MAX(id) FROM question))) ORDER BY id LIMIT 1;


SELECT description FROM question AS t1 JOIN (SELECT ROUND(RAND() * (SELECT MAX(id) FROM question)) AS id) AS t2 WHERE t1.id >= t2.id ORDER BY t1.id ASC LIMIT 1;

阅读更多

没有更多推荐了,返回首页