我試圖通過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