在插入时触发SQL更新

时间:2014-12-21 17:57:47

标签: mysql triggers

我需要一些关于在MySQL上创建触发器的帮助。

我有一张表“评论”:

id_comment | id_topic | comment

和“主题”表:

id_topic | topic | comments_ammount

我需要为“评论”表中的每个插页增加comments_ammount,该表具有与该主题相同的id_topic

我从未使用触发器,所以任何人都可以帮助我?

1 个答案:

答案 0 :(得分:0)

好吧,我在mysql中并不陌生,但我认为你可以做这样的事情

DELIMITER $$
declare @x table(id_topic int)
insert into @x   '@x hold last insert'
select * 
from comment
order by id desc
limit 1
update topic t join @x c 
on c.id_topic =t.id_topic
set  comments_ammount=comments_ammount+1
DELIMITER $$
DELIMITER ;