我试图通过phpMyAdmin来执行数据库中的这个查询MySQL的BEFORE INSERT触发器把重复的主键插入到更新
create trigger avoid_duplicated_sharing
before insert on sharingevents
for each row
begin
if (select count(*) from sharingevents where shared_note_id = NEW.shared_note_id AND shared_to = NEW.shared_to > 0) then
delete from sharingevents where shared_note_id = NEW.shared_note AND shared_to = NEW.shared_to
END IF;
END
但的phpmyadmin给了我以下错误:
MySQL said: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'END IF' at line 7
两个问题:
- 我的脚本有什么问题?
- 经过
BEFORE INSERT
触发后,是否会执行INSERT
操作?如果它没有我将不得不删除INSERT INTO SharingEvents (SELECT * FROM NEW);
你不能转动将其插入到一个更新,你把它变成相同的插入。 – Barmar
你说得对,我把查询改为:'从sharingevents中删除where(shared_note = NEW.shared_note_id AND shared_to = NEW.shared_to);' – Maximetinu
'INSERT INTO ... ON DUPLICATE KEY UPDATE'有什么问题或者INSERT IGNORE'查询?它们不适合你的用例吗? – Mjh