select 
   a.主题,
    count(b.帖子编号) as 所含帖子数量
from
    theme as a inner join article as b on a.theme=b.theme
group by
    a.主题

解决方案 »

  1.   

    如果要做BBS 的话,一般情况都会在帖子这张表加一个字段,冗余那些常查询的列,比如,回贴数、最后回贴人、日期等。不过,不是用 TR 实现的,都在回帖后,马上修改刚才所提到的那几列。个人建议,仅供参考。
      

  2.   

    我的意思是自动增加数目,
    就是每一次增加或或者删除一篇帖子article,然后theme表中的article的数目就相应增加或者减少1。
      

  3.   

    这个确实不需要触发器来做。
    一定要的话可以用下面的 create trigger tr_article 
     on article
     after insert,delete,update
     as begin
      update a set article_num=isnull(b.[count],0)
      from theme a
      join (select theme,count(article) as [count] from article
      group by theme) b on a.theme=b.theme
      where a.articel_num<>isnull(b.[count],0)
     end
      

  4.   

    同意楼上create trigger tr_add_article  -- 增加
     on article 
     after insert
     as 
    begin
    declare @num = select article_num
      from theme 
     update theme
    set article_num= @num+1
     endcreate trigger tr_add_article  -- 删除
     on article 
     after Delete
     as 
    begin
    declare @num = select article_num
      from theme 
     update theme
    set article_num= @num-1
     end
      

  5.   


    create trigger tr_del_article  -- 删除
    名字忘了改了