![](/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.