I am trying to insert players into my database. If a player in a team has the same "Rugnummer" as the player that is being inserted, the player's "Rugnummer" that is being inserted should be changed. Instead the code I have know changes the "Rugnummer" of the player that already exists in the table. Running the INSERT again causes the new player to be inserted with the "Rugnummer" the already existing player had.
I have tried a couple of different things, different pieces of code and changing some things, but nothing seems to be wokring, at all, after changing some things up.
I also tried using triggers, but with an insert OLD.Rugnummer can't be used.
INSERT INTO spelers (Speler_voornaam, Speler_achternaam, Team_ID, Rugnummer)
VALUES ("Bram", "Bakker", 1, 28)
ON DUPLICATE KEY UPDATE Rugnummer =
(select (t*10+u+1) x from (select 0 t union select 1 union select 2 union select 3 union select 4) A,
(select 0 u union select 1 union select 2 union select 3 union select 4 union
select 5 union select 6 union select 7 union select 8 union select 9) B
WHERE (t*10+u+1) NOT IN (SELECT Rugnummer
FROM spelers AS s
WHERE Team_ID = 1)
ORDER BY x
LIMIT 1
The Following is an insert I tried. I expect number 1 through 10 and letter A till J. What actually is shown is A-A with the "Rugnummer" 12 and J-J is not shown. A-A is changed instead of J-J (These have the same "Rugnummer)
INSERT INTO spelers (Speler_voornaam, Speler_achternaam, Team_ID, Rugnummer)
VALUES
("A", "A", 19, 1),
("B", "B", 19, 2),
("C", "C", 19, 3),
("D", "D", 19, 4),
("E", "E", 19, 5),
("F", "F", 19, 6),
("G", "G", 19, 7),
("H", "H", 19, 8),
("I", "I", 19, 9),
("J", "J", 19, 1)
ON DUPLICATE KEY UPDATE Rugnummer =
(select (t*10+u+1) x from (select 0 t union select 1 union select 2 union select 3 union select 4) A,
(select 0 u union select 1 union select 2 union select 3 union select 4 union
select 5 union select 6 union select 7 union select 8 union select 9) B
WHERE (t*10+u+1) NOT IN (SELECT Rugnummer
FROM spelers AS s
WHERE Team_ID = 19)
ORDER BY x
LIMIT 1)
The expected result is that the new player is inserted with a not yet used "Rugnummer" instead of needing to use the insert statement twice and changing the "Rugnummer" of the already existing player.