mysql - sql怎么随机取50条数据?
天蓬老师
天蓬老师 2017-04-17 13:40:55
0
7
671

MySQL从一张表怎么随机取50条数据?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(7)
黄舟
select * from table order by rand() limit 50;
阿神

SELECT * FROM table_name ORDER BY rand() LIMIT 50;

小葫芦
SELECT *
FROM 表名 AS t1
JOIN (
SELECT ROUND( RAND( ) * (SELECT MAX( id )FROM 表名 ) ) AS id
) AS t2
WHERE t1.id >= t2.id
ORDER BY t1.id ASC
LIMIT 50
阿神

This thing needs to be looked at separately.
If your table records do not have tens of thousands levels. Then order by rand() is also acceptable.
But when the table records are large, order by rand() the efficiency is extremely low.
So it is recommended to explain the business scenarios as much as possible on the basis of asking questions~~~.

左手右手慢动作

order by rand()

洪涛

rand(), I vaguely remember that it was repeatedly emphasized in "High Performance MySQL" to avoid using it.
Based on the actual situation, if the data volume is not large, tens of thousands or hundreds of thousands, it can be used;
No matter how large it is, it is better to use a program to implement it, and then query it.

黄舟

rand() is fine, but please remember not to use SQL statements containing any calculations in online projects.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template