求一个SQL排序语句。

来源:百度知道 编辑:UC知道 时间:2024/09/24 03:46:15
现在二张表,现分别叫 t1 t2
t1内有如下字段:fd1, t2ID, fd3, ...
t2内有如下字段:id, fd1, fd2, ...

我可以使用如下语句查出t1表中按t2ID记录数的多少从大到小的结果。
select t2ID,count(t2ID) c from t1 group by t2ID order by c desc

查出来的t2ID是表t2中的ID字段。

我现在想顺序不变,通过t2ID能得到t2表中的所有字段(fd1,fd2)

大家注意这里的顺序是刚才查出来的顺序.

以上问题1.
=======================
问题2。
我查询的时候要分页查询。
就是说我给定参数(页数,每页记录数)返回指定页数的记录。。

麻烦大家给我写一个存储过程可以吗?

麻烦大家了,解决问题必有重谢!!

刚才那个没问题没问好,和这个也是一回事,解决了一并给分。。
http://zhidao.baidu.com/question/59056194.html
如有对问题不明白的可以随时问我,Q:46029811

1.select t2.* from t2 inner join (select t2ID,count(t2ID) c from t1 group by t2ID order by c desc) tt on t2.id=tt.t2id order by tt.t2id
2.
declare @firstrow int,@lastrow int
select @firstrow=(@页数-1)*@每页记录数+1,@lastrow=@页数*@每页记录数
select row_number() over(group by t2.id) as rownum,t2.* from t2 inner join (select t2ID,count(t2ID) c from t1 group by t2ID order by c desc) tt on t2.id=tt.t2id where rownum between @firstrow and @lastrow order by tt.t2id

应该是这个意思把?
select d.id,d.fd1,d.fd2,b.c from t2 d, (select t2ID,count(t2ID) c from t1 group by t2ID) b
where d.id=b.t2ID order by b.c desc