![](/img/trans.png)
[英]Get records from last hour or last 20 items if there is none in the last hour
[英]return random 5 records from last 20 records
我只有以下内容
有记录的表,我想从最后20条记录中随机返回5条记录(按照id desc排序)
所以我们如何能够快速地做到这一点感谢你的帮助
select * from
(
select * from your_table
order by id desc limit 20
) as lastest_results
order by rand()
limit 5;
使用内部查询返回最后20个,使用外部查询随机选择其中的5个。 这可能会很慢。
SELECT * FROM (SELECT * FROM table ORDER BY id DESC LIMIT 20) t ORDER BY RAND() LIMIT 5;
你需要类似的东西
SELECT * FROM table ORDER BY RAND() LIMIT 5;
如果你有一个时间参数来排序它们,例如最后20条记录,那么就使用它。
SELECT * FROM table ORDER BY insert_time DESC, RAND() LIMIT 5;
要么
SELECT * FROM表ORDER BY id DESC,RAND()LIMIT 5;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.