我需要一些有关在 MySQL 上创建触发器的帮助。
我有一个表“评论”:
id_comment | id_topic | comment
和“主题”表:
id_topic | topic | comments_ammount
我需要为“评论”表上的每个插入增加 comments_ammount
,该表具有相同的主题 id_topic
。
我从来没有使用过触发器,所以有人可以帮助我吗?
最佳答案
嗯,我对 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 ;
关于mysql - 插入时触发 SQL 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27592140/