theme(theme , introuduction , article_num) 主题(主题,介绍,所含帖子数量)
article(article_id,theme ) 帖子(帖子编号,所属主题)
现在要写一个触发器,在主题那个表中显示该主题所含帖子的数量。
小白求教。。
------解决方案--------------------
select
a.主题,
count(b.帖子编号) as 所含帖子数量
from
theme as a inner join article as b on a.theme=b.theme
group by
a.主题
------解决方案--------------------
这个确实不需要触发器来做。
一定要的话可以用下面的
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
------解决方案--------------------
同意楼上
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
end
create 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