sql语句如何根据另一个表进行排序

A表里有H字段(每条数据都是唯一的 INT类型)B表里也有H字段(值和A表里一样,每条数据都可以重复 INT类型)我需要列出A表里面的数据,排序是根据B表里面H字段的重复量的多少。

select a.*
from a
left join
(
select h,count(*) as c_num
from b
group by h
)t
on a.h=t.h
order by t.c_num desc/asc以B表H字段的重复量来对A表的数据进行排序,至于是升序还是降序,楼主根据自己的需求吧
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-09-13
create table ttt(t1 int)
insert some values to ttt;
select t1 from ( select t1 from ttt group by t1 ) where rownum < 5

以为group by 会自动排序