插入后我需要一个mysql触发器或者更新我要添加到表通知的表(以及将来发送电子邮件通知)
触发插入工作正常 触发器更新有问题,如果我编辑“word”他正确插入,但如果我再次编辑...我收到一个错误,因为e尝试在表通知中插入相同的id
table_real
-------------
| id | word | news |
-------------
| 1 | this | this |
-------------
| 2 | that | this |
-------------
| 3 | this | this |
-------------
tablenotifications
---------------------
| id | word | news |
---------------------
| 1 | this | 2 |
---------------------
| 2 | that | 1 |
DELIMITER $$
CREATE
TRIGGER `trigger_insert` AFTER INSERT
ON `table_real`
FOR EACH ROW BEGIN
INSERT INTO tablenotifications(id, word, news) VALUES (new.id, new.word, new.news);
END$$
DELIMITER ;
DELIMITER $$
CREATE
TRIGGER `trigger_update` AFTER UPDATE
ON `table_real`
FOR EACH ROW BEGIN
INSERT INTO tablenotifications (id, word, news) VALUES (new.id, new.word, new.news);
END$$
DELIMITER ;
解决方案: 编辑表格,更改我的主键
答案 0 :(得分:0)
当生成表而不是插入时,更新与编辑的id对应的行。
DELIMITER $$
CREATE
TRIGGER `trigger_update` AFTER UPDATE
ON `table_real`
FOR EACH ROW BEGIN
UPDATE tablenotifications SET id=new.id, word=new.word, news=new.news;
END$$