select top 20 total_s.sid,total=Count(*),selfmusic.username,selfmusic.music,selfmusic.intro,userinfo.photo 
from total_s,selfmusic,userinfo 
where total_s.sid=selfmusic.id 
--這裡改一下total=Count(*),改成 total=(select count(*) from total_s...)

解决方案 »

  1.   

    在前台的.net程序里,需要分段显示30至50、50至70的记录。
    但是显示的时候有相同记录,十分恼火。哎~~
    想加一个唯一的标识。但是不知道怎么加。或者能区分出30至50里是否已经有过那条记录,后面就不显示了。比如total字段跟sid为
    sid   total
     a      1
     b      1
     c      1
     d      1
    .......如果有很多条total都是1    那么就有可能在30-50,还有50-70都有显示同一记录
    比如  c    1     ....
      

  2.   

    你将你的数据加入一个自增列放到一个临时表中去。类似  Select ID=Identity(Int,1,1),* Into #T from tblName然后Select * from #T Where ID Between 31 And 50即为30-50 的记录Select * from #T Where ID Between 51 And 70即为50-70 的记录。
      

  3.   

    select ID=Identity(Int,1,1),total_s.sid,total=Count(*),selfmusic.username,selfmusic.music,selfmusic.intro,userinfo.photo 
    Into #T
    from total_s,selfmusic,userinfo 
    where total_s.sid=selfmusic.id 
    and selfmusic.username=userinfo.username 
    Group By total_s.sid,selfmusic.username,selfmusic.music,selfmusic.intro,userinfo.photo 
    Order by total DESC我这么建出来是按照total排序的,id字段依然是乱排的。 between也就没有意义了。怎么搞?
      

  4.   

    按照Total排序,但是ID是自增的啊,不可能自己会乱掉啊。它的顺序是1,2,3,4....Select * from #T Where ID Between 31 And 50 Order By ID 就应该可以得到30-50 的记录
      

  5.   

    按照上面的语句建出来是
    id       sid    total           ....75 42 4             ....
    1 73 3             ....
    51 1119 3             ....
    64 1154 3             ....
    63 1153 2             ....
    61 1150 2             ....
    57 1135 2             ....
    有没有不建临时表的办法啊?
      

  6.   

    不是啊,你在插入临时表的时候就将Total排序啊。select ID=Identity(Int,1,1),total_s.sid,total=Count(*),selfmusic.username,selfmusic.music,selfmusic.intro,userinfo.photo 
    Into #T
    from total_s,selfmusic,userinfo 
    where total_s.sid=selfmusic.id 
    and selfmusic.username=userinfo.username 
    Group By total_s.sid,selfmusic.username,selfmusic.music,selfmusic.intro,userinfo.photo 
    Order by total DESC
    这样得出来的ID会是你上面那样子吗??应该是排序好的才对啊。
      

  7.   

    你在插入临时表的时候加上Order by total DESC还是会那样,那就奇怪了。
      

  8.   

    -------------------------------------------
    我把问题简化一下select top 1 total_s.sid,total=Count(*),selfmusic.username,selfmusic.music,selfmusic.intro,userinfo.photo 
    from total_s,selfmusic,userinfo 
    where total_s.sid=selfmusic.id 
    and selfmusic.username=userinfo.username 
    Group By total_s.sid,selfmusic.username,selfmusic.music,selfmusic.intro,userinfo.photo 
    Order by total DESC
    select top 1 total_s.sid,total=Count(*),selfmusic.username,selfmusic.music,selfmusic.intro,userinfo.photo 
    from total_s,selfmusic,userinfo 
    where total_s.sid=selfmusic.id 
    and selfmusic.username=userinfo.username 
    and total_s.sid 
    not in(Select sid from (Select top 2 sid,Count(*) As total from total_s Group By sid Order by total DESC) A) 
    Group By total_s.sid,selfmusic.username,selfmusic.music,selfmusic.intro,userinfo.photo 
    Order by total DESC这两条记录提不出来用户名为igex的记录,只能显示 nils 跟 tttsid  total  username73     4     nils
    42     4     igex
    19     3      ttt
      

  9.   

    没办法,用了真实表T1的增字段
    insert into t1(sid,total,username,music,intro,photo) 
    select total_s.sid,total=Count(*),selfmusic.username,selfmusic.music,selfmusic.intro,userinfo.photo 
    from total_s,selfmusic,userinfo 
    where total_s.sid=selfmusic.id 
    and selfmusic.username=userinfo.username 
    Group By total_s.sid,selfmusic.username,selfmusic.music,selfmusic.intro,userinfo.photo 
    Order by total DESC每次truncate table t1   然后用between
    谢谢鱼。放分