So I've tried a whole lot of diffrent things with If statements, and i simply can't seem to work out how to do this.
I wan't to make the trigger delete the row with the lowest ID, if it recurring. And by recurring i mean the number and username are the same in two rows.
For example
ROW1: ID: 1 , Nr: 1 , UN: MVJ and
Row2: ID: 2 , Nr: 1 , UN: MVJ
Those are recurring, but if the 'Nr'
or 'UN'
were different, they wouldn't be.
So here is my try so far.
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;
Found the right syntax, but found out the move was impossible in SQL. You can't delete a row from the same the table you select which row to delete.
The right syntax was:
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 ;