T1: 
bbsid user neirong  count
1122   aa   吃饭去了  0T2 
REID bbsid     replay 
1     1122    什么时候回来啊
2     1122    马上回来
 
主要功能就是为了统计贴子的回复数目 ,类似于CSDN,
t2表贴子有几条回复数,就在T1表count相加数目,如现在上表,现在COUNT应该是2用触发器吗?怎么写?

解决方案 »

  1.   

    create trigger tri_t2
    on t2
    for insert
    as
    begin
       update a set a.count=b.count
       from t1 a join (
       select bbsid,count(*) as count from inserted group by bbsid
       )b on a.bbsid=b.bbsid
    end
      

  2.   

    这效果,直接查询就行了,触发器不好select bbsid,user,neirong,count=(
    select count(*) from t2 where bbsid=t.bbsid
    )
    from t1 t 
      

  3.   

    bbsid=t.bbsid  这个t.bbsid是指哪个表啊?t2??
      

  4.   


    那这有两个t    from t1 t            bbsid=t.bbsid
      

  5.   

    select bbsid,user,neirong,count=( 
    select count(*) from t2 where bbsid=t1.bbsid 
    ) from t1运行提示:"from附件有语法错误"  "count附件有语法错误"
      

  6.   

    create trigger tri_t2
    on t2
    for insert
    as
    begin
       update a set a.count=b.count
       from t1 a join (
       select bbsid,count(*) as count from inserted group by bbsid
       )b on a.bbsid=b.bbsid
    end
    完全可以
      

  7.   


    declare @t1 table (bbsid int ,[user] nvarchar(10),neirong nvarchar(10),count int)
    insert into @t1 select 1122,'aa','吃饭去了',0 
    select * from @t1
    declare @t2 table (reid int,bbsid int,replay nvarchar(10))
    insert into @t2 select 1,1122,'什么时候回来啊'
          union all select 2,1122,'马上回来'
    update a set a.[count]=b.[count] from @t1 a join      
    (select t2.bbsid,COUNT(*) as [count] from @t2 t2 join @t1 as t1  on t2.bbsid=t1.bbsid
                     group by t2.bbsid) as b on a.bbsid=b.bbsid
    select * from @t1
    ------
    1122 aa 吃饭去了 2
    ------