--建立测试数据 create table 文章表(ID int,回复的父ID int,加分 int,作者 int) insert 文章表 select 1,0,3,3 union all select 2,1,2,4 union all select 3,1,1,5select 主题文章id,主题文章的总得分,b.作者 as 最后回复者,回复次数 from (select 回复的父ID as 主题文章id , sum(加分)+3 as 主题文章的总得分, maxid=max(id) ,count(id) as 回复次数 from 文章表 where 回复的父ID>0 group by 回复的父ID)a inner join 文章表 b on a.maxid=b.id--结果 主题文章id 主题文章的总得分 最后回复者 回复次数 ----------- ----------- ----------- ----------- 1 6 5 2 --删除测试数据 drop table 文章表
1 0 3 3
2 1 2 4
3 1 1 5
--建立测试数据
create table 文章表(ID int,回复的父ID int,加分 int,作者 int)
insert 文章表 select 1,0,3,3
union all select 2,1,2,4
union all select 3,1,1,5select 主题文章id,主题文章的总得分,b.作者 as 最后回复者,回复次数 from
(select 回复的父ID as 主题文章id , sum(加分)+3 as 主题文章的总得分,
maxid=max(id) ,count(id) as 回复次数 from 文章表 where 回复的父ID>0 group by 回复的父ID)a inner join 文章表 b
on a.maxid=b.id--结果
主题文章id 主题文章的总得分 最后回复者 回复次数
----------- ----------- ----------- -----------
1 6 5 2
--删除测试数据
drop table 文章表