请教一条sql语句,希望大侠们进来看看。

njstalk 2009-02-27 02:53:56
有如下一个表结构,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万,用什么方法比较快捷呢?
万分感谢!
...全文
129 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhongxiutao123 2009-02-27
  • 打赏
  • 举报
回复
SQL语句估计没什么优化的余地了;建议在该表的 pid 和 parentid 字段上建立索引。
子陌红尘 2009-02-27
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 njstalk 的回复:]
大侠出手果然厉害啊。呵呵。
想请问下,有更好的方法不啊,因为如果有上百万条记录需要处理的时候就有些慢了。
谢谢!
[/Quote]

SQL语句估计没什么优化的余地了;建议在该表的 pid 和 parentid 字段上建立索引。
njstalk 2009-02-27
  • 打赏
  • 举报
回复
大侠出手果然厉害啊。呵呵。
想请问下,有更好的方法不啊,因为如果有上百万条记录需要处理的时候就有些慢了。
谢谢!
子陌红尘 2009-02-27
  • 打赏
  • 举报
回复

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
*/
njstalk 2009-02-27
  • 打赏
  • 举报
回复
论坛把很多空格去掉了。
字段有这些:pid、tid、parentid、layer、poster、posterid
字段的解释在原帖子有。
dobear_0922 2009-02-27
  • 打赏
  • 举报
回复
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
dawugui 2009-02-27
  • 打赏
  • 举报
回复
好象就你这一个表,写不出来吧?


请给出表结构,测试数据,相关算法和需要的结果.谢谢!

dawugui 2009-02-27
  • 打赏
  • 举报
回复
用户名的字段是:poster ?
子陌红尘 2009-02-27
  • 打赏
  • 举报
回复
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

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧