create table BBSUserDate
(
    userDateId int identity(1,1) primary key,
userId int not null ,
nickName nvarchar(50)not null --用户昵称--
)
go
/*用户主贴表*/
if exists(select 1 from sysObjects where name='BBSTopic')
drop table BBSTopic
go
create table BBSTopic
(
topicId int identity(1,1) primary key,
         postPerId int not null, --FK 发帖人--
clickCount int not null default(0), --浏览数量--
replyCount int not null default(0), --回帖数量--
topicTitle varchar(50) not null, --帖子标题--
TopicContent varchar(4000) not null, --帖子内容--
publishTime datetime  not null default(getdate()), --发帖时间--
state int not null default(0),   --帖子状态--
)
go/*回帖表*/
if exists(select 1 from sysObjects where name='BBSReply')
drop table BBSReply
go
create table BBSReply
(
replyId int identity(1,1) primary key,
repPerId int not null,    --回帖人--
replyContent nvarchar(1000) not null,  --帖子内容--
publishTime datetime not null default(getdate()) ,  --回帖时间--
state int not null default(0),   --帖子状态--
topicId int not null
)go像csdn这样的帖子列表的sql语句怎么写啊。想了很久都写不出来。麻烦大家帮帮忙啊.标题  提问人  回复数/点击数  最后回复人/回复时间 就需要上面的几个列的数据。 我只能用2个sql语句才能完成查询,不知道怎么用1条sql语句搞定。

解决方案 »

  1.   

    最好给出完整的表结构,测试数据,计算方法和正确结果.发帖注意事项
    http://topic.csdn.net/u/20091130/21/fb718680-98ff-4afb-98d8-cff2f8293ed5.html?24281
      

  2.   

    select
      b.topicTitle,b.postPerId,b.replyCount,b.clickCount,c.repPerId,c.publishTime
    from
      BBSUserDate a,
      BBSTopic b,
      (select repPerId,max(publishTime) as publishTime from BBSReply group by repPerId)c
    where
      a.userId=b.postPerId and a.userId=c.repPerId
      

  3.   


    insert into BBSTopic values(1,60,123,'测试1','前正直炎夏酷暑,气温很高,但是学员的学习热情不减。在今后半个月的时间里,学员们将就家',default, 0)
    insert into BBSTopic values(1,60,123,'测试2','前正直炎夏酷暑,气温很高,但是学员的学习热情不减。在今后半个月的时间里,学员们将就家',default, 0)insert into BBSUserDate values(1,'管理员');
    insert into BBSUserDate values(2,'Mile');
    insert into BBSReply values(2,'回复测试1',default,0,1)
    insert into BBSReply values(2,'回复测试2',default,0,2)
    效果如下:
    标题     提问人   回复数/点击数    最后回复人/回复时间 
    测试1   管理员   0       0      Mile  2009-12-18 12:12:12