所以我用If語句試了很多不同的東西,而且我似乎無法解決如何做到這一點。如果在刪除觸發器中的聲明 - 失敗
我不想讓觸發器刪除ID最低的行,如果它重複發生。 而通過重複我的意思是數字和用戶名是相同的兩行。
例如
ROW1: ID: 1 , Nr: 1 , UN: MVJ and
Row2: ID: 2 , Nr: 1 , UN: MVJ
這些是重複的,但如果'Nr'
或'UN'
是不同的,他們不會。
所以這裏是我嘗試到目前爲止。
CREATE TRIGGER no_double_reservations
AFTER INSERT ON tilmeldte
FOR EACH ROW
WHERE
'SELECT COUNT(*) from tilmeldte WHERE (kursus_nr, username) IN (SELECT kursus_nr, username FROM tilmeldte GROUP BY kursus_nr, username HAVING count(*) = 2)' = '2'
DELETE from tilmeldte Where tilmeldingsid =
'Select min(`tilmeldingsid`) from tilmeldte WHERE (kursus_nr, username) IN (SELECT min(kursus_nr), username FROM tilmeldte GROUP BY kursus_nr, username HAVING count(*) = 2)'
END;
找到了正確的語法,但發現此舉在SQL中是不可能的。 您不能從選擇要刪除的行的同一表中刪除一行。
正確的語法是:
DELIMITER !!
CREATE TRIGGER no_double_reservations
AFTER INSERT ON tilmeldte
FOR EACH ROW BEGIN
IF(
SELECT COUNT(*) from tilmeldte WHERE (kursus_nr, username) IN (SELECT kursus_nr, username FROM tilmeldte GROUP BY kursus_nr, username HAVING count(*) = 2) = 2)
THEN
DELETE from tilmeldte Where tilmeldingsid = (Select min(`tilmeldingsid`) from tilmeldte WHERE (kursus_nr, username) IN (SELECT min(kursus_nr), username FROM tilmeldte GROUP BY kursus_nr, username HAVING count(*) = 2));
END IF;
END!!
DELIMITER ;
爲什麼你的查詢周圍有單引號? – 2014-10-02 22:28:30