简体   繁体   中英

SQL Delete from two tables

I have two tables: 'player' and 'game'. The primary key of player is the foreign key in the table game.

'player' --> PK = idPlayer
'game'   --> FK = player_idPlayer

So now I want to delete one player in the players table. The SQL statement should also delete all entries of the game table where idPlayer = player_idPlayer - if there are some.

My statement only deletes when the player is in both tables. But i want to delete the player also when this player is not in the game table.

DELETE player, game 
FROM players, game 
WHERE idPlayer = player_idPlayer AND player_idPlayer = ?

You could try with a left outer join

DELETE p, g 
FROM players p 
LEFT OUTER JOIN game g ON g.player_idPlayer = p.idPlayer
WHERE p.idPlayer = ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM