大家好,我是考100分的小小码 ,祝大家学习进步,加薪顺利呀。今天说一说mysql随机抽取数据50条_sql随机取若干条数据,希望您对编程的造诣更进一步.
-- 慢 SELECT * FROM table_name ORDER BY rand() LIMIT 5; -- 较慢 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; -- 快 `table 有 id 字段 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; -- 快 `table 没有有 id 字段 select * from (select @rownum:=@rownum + 1 as id,value from `table`,(select @rownum:=0) as a) as t1 join ( select round( rand() * ( (select max(b.id) from (select @rownum_max:=@rownum_max + 1 as id,value from `table`,(select @rownum_max:=0) as a) as b ) - (select min(b.id) from (select @rownum_min:=@rownum_min + 1 as id,value from `table`,(select @rownum_min:=0) as a) as b ) )) + (select min(b.id) from (select @rownum_min1:=@rownum_min1 + 1 as id,value from `table`,(select @rownum_min1:=0) as a) as b ) as id ) as t2 on t1.id>= t2.id order by t1.id limit 1
代码100分
缺点:
每次查询后会获得连续的n条数据
解决办法:
每次查一条数据,重复查询n 次
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/9816.html