有如下一个表结构,pid 是帖子id,tid 是主题id,parentid是父id。
当layer=0时 表示这一个是主题的第1楼帖子,就是楼主发的;
当layer=1时 表示这个回帖是回个这个主题的,我们算做是回给楼主的;
当layer=2时 表示这个回帖是回个parentid所指代的帖子的人的。
tablename
pid tid parentid layer poster           posterid
4734182 363580 4734182          0 user01             34550
4734192 363580 4734192          1 user02         40131
4734195 363580 4734195          1 user03               22843
4734198 363580 4734195          2 user01             34550
4734203 363580 4734203          1 user04             38269现在有个题目,就是统计每个被哪些用户回了多少次贴。
形成如下的表格(示例):用户名     回复人  次数
user1     user2   4
user1     user3   5
user2     user1   10
...
请问应该怎样写sql语句呢?
麻烦大侠们帮帮忙啊!
我现在想到的方法大概是这样的:
1、 select user2 from tablename where layer=1 and tid in(select tid from tablename where lager=0 and posterid = 'user1')
找到user2对user1 发起的主题的直接回复。
2、select user2 from tablename where layer=2 and tid in(select tid from tablename where lager=1 and posterid = 'user1')
找到user2对user1 回帖的回复。
然后再把两个连起来。然后再重复每个用户,这个方法很笨,而且数据量大的时候相当慢,如果用户数量较多,超过1万,用什么方法比较快捷呢?
万分感谢!

解决方案 »

  1.   

    try:
    select
        t.username,t.replyer,count(*) as times
    from
        (select a.poster as username,b.poster as replyer from tabname a,tabname b where a.tid=b.tid and a.layer=0 and b.layer=1
         union all
         select a.poster,b.poster from tabname a,tabname b where a.tid=b.tid and b.layer>1 and a.pid=b.parentid) t
    group by
        t.username,t.replyer
      

  2.   

    select t.username,t.replyer,count(*) as times
    from (select a.poster as username,b.poster as replyer from tabname a,tabname b where a.tid=b.tid and a.layer=0 and b.layer=1
         union all
         select a.poster,b.poster from tabname a,tabname b where a.tid=b.tid and b.layer>1 and a.pid=b.parentid) t
    group by t.username,t.replyer
      

  3.   

    论坛把很多空格去掉了。
    字段有这些:pid、tid、parentid、layer、poster、posterid
    字段的解释在原帖子有。
      

  4.   


    declare @t table(pid int,tid int,parentid int,layer int,poster varchar(10),posterid int)
    insert into @t select 4734182,363580,4734182,0,'user01',34550 
    insert into @t select 4734192,363580,4734192,1,'user02',40131 
    insert into @t select 4734195,363580,4734195,1,'user03',22843 
    insert into @t select 4734198,363580,4734195,2,'user01',34550 
    insert into @t select 4734203,363580,4734203,1,'user04',38269 select
        t.username,t.replyer,count(*) as times
    from
        (select a.poster as username,b.poster as replyer from @t a,@t b where a.tid=b.tid and a.layer=0 and b.layer=1
         union all
         select a.poster,b.poster from @t a,@t b where a.tid=b.tid and b.layer>1 and a.pid=b.parentid) t
    group by
        t.username,t.replyer
    order by
        t.username,t.replyer/*
    username   replyer    times       
    ---------- ---------- ----------- 
    user01     user02     1
    user01     user03     1
    user01     user04     1
    user03     user01     1
    */
      

  5.   


    SQL语句估计没什么优化的余地了;建议在该表的 pid 和 parentid 字段上建立索引。
      

  6.   

    SQL语句估计没什么优化的余地了;建议在该表的 pid 和 parentid 字段上建立索引。