嗨,我的表格是这样的:
current | similar
8 |2
8 |4
8 |4
8 |4
8 |4
8 |5
8 |5
但我想以这种方式首先4,因为它重复4次,5秒cuz重复两次,最后2次:
current | similar
8 |4
8 |4
8 |4
8 |4
8 |5
8 |5
8 |2
我试过:
select * from mytable order by similar
,但我先得到5分4秒,最后2分
也:
select * from mytable order by count(similar) but only one row is returned
最后:
select *, count(similar) as a from mytable order by a
,但即使是小组也没有工作,我该如何实现呢?感谢
答案 0 :(得分:3)
首先,您可以计算similar
组中每个current
字段的出现次数,然后使用原始表格join
对其进行排序,以对结果集进行排序。
select t1.current, t1.similar
from tbl t1
join (
select current, similar, count(*) as ttl
from tbl
group by current, similar
) t2 on t1.current = t2.current and t1.similar = t2.similar
order by t1.current, t2.ttl desc