[英]return random 5 records from last 20 records
Simply I have the following 我只有以下内容
table with records , i want to return randomly 5 records from the last 20 record (order by id desc) 有记录的表,我想从最后20条记录中随机返回5条记录(按照id desc排序)
so how we can do it fast thanks for help. 所以我们如何能够快速地做到这一点感谢你的帮助
select * from
(
select * from your_table
order by id desc limit 20
) as lastest_results
order by rand()
limit 5;
Use an inner query to return the last 20, and an outer query to select 5 of them randomly. 使用内部查询返回最后20个,使用外部查询随机选择其中的5个。 This may be slow though.
这可能会很慢。
SELECT * FROM (SELECT * FROM table ORDER BY id DESC LIMIT 20) t ORDER BY RAND() LIMIT 5;
This is a slow method but it gets the JOB done: 这是一个缓慢的方法,但它完成了JOB:
ORDER BY RAND()
LIMIT 5;
If you are using a large table this can become very slow, There are multiple alternative that you can read about here 如果您使用的是大型桌子,这可能会变得很慢。您可以在此处阅读多种替代方案
You need something like 你需要类似的东西
SELECT * FROM table ORDER BY RAND() LIMIT 5;
If you have a time parameter to sort them by for example the last 20 records then use this. 如果你有一个时间参数来排序它们,例如最后20条记录,那么就使用它。
SELECT * FROM table ORDER BY insert_time DESC, RAND() LIMIT 5;
OR 要么
SELECT * FROM table ORDER BY id DESC, RAND() LIMIT 5; SELECT * FROM表ORDER BY id DESC,RAND()LIMIT 5;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.