日期:2014-05-19  浏览次数:20365 次

在线等, 求一个得到主贴以及回帖相关的sql语句, 谢谢.
是这样的.   主贴和回帖分别用2个表,   MainThread和Thread
现在求一sql语句,   想得到所有主贴,   以及每个主贴的回帖数量和最后回复时间
谢谢了.

------解决方案--------------------
declare @主帖 table(id int identity(1,1),name varchar(100))
declare @回帖 table(id int identity(1,1),pid int,name varchar(100),replytime datetime)


insert @主帖(name)
select '第1封帖 ' union all
select '第2封帖 '

insert @回帖(pid,name,replytime)
select '1 ', '第1封帖的第1回复 ', '2007-7-1 11:00 ' union all
select '1 ', '第1封帖的第2回复 ', '2007-7-1 12:00 ' union all
select '2 ', '第2封帖的第1回复 ', '2007-7-1 11:00 ' union all
select '2 ', '第2封帖的第2回复 ', '2007-7-1 14:00 ' union all
select '2 ', '第2封帖的第3回复 ', '2007-7-1 15:00 '

select * from @主帖
select * from @回帖

select
a.name,
count(*),
max(replytime)
from @主帖 a
inner join @回帖 b
on a.id = b.pid
group by a.name