简体   繁体   English

从两个表中删除SQL

[英]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. SQL语句还应该删除游戏表中的所有条目,其中idPlayer = player_idPlayer如果有的话。

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 = ?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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