string strsql = "select post.postid,post.post_title,post.name,post.class,post.time,reply.name,count(replyid) as aaaa from post LEFT outer JOIN reply ON( post.postid=reply.postid) group by post.postid,post.post_title,post.name,post.class,post.time"各位大虾,这条语句目的是要查询帖子标题,帖子的id,帖子的回复数量,现在我还想查询最后回复人的姓名怎么做啊
表 post   
  列名postid post_title post name time class   
表 reply
  列名replyid reply postid time name
 

解决方案 »

  1.   

    select 回复人 from table order by Time descselect 回复人 按时间排序
      

  2.   


    select *, (select  top 1   name from reply    where  a.postid=postid    order by Time desc) as names   from  post   a    
      

  3.   


    select a.postid,a.post_title,a.name,a.class,a.time,(select   count(*) from reply    where  a.postid=postid) as 回复总数 ,(select  top 1   name from reply    where  a.postid=postid    order by Time desc) as 最后回复人姓名 from  post   a
      

  4.   

    select top 1 post.postid,post.post_title,post.name,post.class,post.time,reply.name,count(replyid) as aaaa 
    from post LEFT outer JOIN reply ON( post.postid=reply.postid) 
    group by post.postid,post.post_title,post.name,post.class,post.time
    order by post.time desc
      

  5.   

    上面都对,建议post表(帖子表)里增加2个字段,最后回复人和最后回复时间,这样就不用这么费劲的查询了。
      

  6.   

    select 帖子标题, 帖子id from 帖子
    select count(*) as  帖子数量 from  帖子
    select 姓名 from 帖子 order by 时间 desc
      

  7.   

    三楼威武符合我的想法,谢了,四楼的方法是有问题的,每次只能查询到一条post记录,而不是所有的post记录,至于八楼,我之前就是用的你这种方法,但是感觉这样会增加数据库量,一张表还好,我的数据库中有N个类似关系,总不能这样吧总之呢,还是灰常感谢大家的帮助
      

  8.   

    论坛回复的话,最好是有人回复的话,更新主帖子相应的字段。这些字段有:回复次数/回复时间/回复人/等。
    这么读帖子列表的时候,就简单了。order 回复时间 desc就可以了。这样性能也可以提高,因为帖子列表看的人多,回复的人少。有问题请到我的博客留言,我会及时回复的。