在 MySQL 中按某些列和 rand() 排序 [英] Sorting by some column and also by rand() in MySQL
本文介绍了在 MySQL 中按某些列和 rand() 排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以按某些列和 RAND() 对结果集进行排序?
Is it possible to sort a result set by some column and also by RAND()?
例如:
SELECT `a`, `b`, `c`
FROM `table`
ORDER BY `a` DESC, RAND()
LIMIT 0, 10
谢谢.
推荐答案
你正在做的事情是有效的 - 它将按 a
降序排列结果,但会随机化关系的顺序.
What you are doing is valid - it will order the results in descending order by a
but randomize the order of ties.
但是,要执行您想要的操作,您首先需要使用子查询获取最新的 100 条记录,然后使用外部查询对该子查询的结果进行随机排序:
However to do what you want you need to first use a subquery to get the latest 100 records and then afterwards sort the results of that subquery randomly using an outer query:
SELECT * FROM
(
SELECT * FROM table1
ORDER BY date DESC
LIMIT 100
) T1
ORDER BY RAND()
这篇关于在 MySQL 中按某些列和 rand() 排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文