MySQL轮流排序 [英] MySQL Sorting by turns
本文介绍了MySQL轮流排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要按以下顺序对以下数据项进行排序:
I want to sort the following data items in the order they are presented below :
id | res
--------
1 A
2 A
3 A
4 A
5 A
6 B
7 B
8 B
9 B
10 B
11 C
12 C
13 C
14 C
15 C
我想要的输出是:
res
---
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
有什么技巧可以使它更正确地排序?
Any tricks to make it sort more properly?
非常感谢!
推荐答案
您可以按组中的等级"排序.可以使用自连接并计算具有较小ID的条目来计算排名.
You can order by the "rank" within the group. The rank can be calculated using a self join and counting the entries with lesser ids.
select t1.id, t1.res
from mytable t1
join mytable t2
on t2.res = t1.res
and t2.id <= t1.id
group by t1.id, t1.res
order by count(*), t1.res
http://rextester.com/ICHZNN18920
这篇关于MySQL轮流排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文