我在MYSQL中有两个表:

Encounters
+----+--------+------------+
| Id | GameId | LocationId |
+----+--------+------------+
| 1  | 5      |     10     |
| 2  | 5      |     12     |
| 3  | 5      |     45     |
+----+--------+------------+
EncounterCards
+----+-------------+--------+
| Id | EncounterId | CardId |
+----+-------------+--------+
|  6 | 1           | 26     |
|  7 | 2           |  5     |
|  8 | 3           |  5     |
+----+-------------+--------+


我正在尝试加入两个表,并删除EncounterCards表中GameId = 5的所有记录。当我进行内部联接时,似乎并没有让我这样做,因为它正在寻找GameId表中的EncounterCards列,但它仅在Encounters表中。

因此,在此用例中,它将从EncounterCards表中删除所有记录。

这是我的查询:

`$query = "DELETE FROM EncounterCards INNER JOIN Encounters ON Encounters.Id = EncounterCards.EncounterId WHERE Encounters.GameId = {$game_id}";`

最佳答案

如果在您的情况下使用INNER JOIN不那么重要,则还可以使用以下命令:

DELETE FROM EncounterCards
WHERE EncounterId IN (
    SELECT id FROM Encounters
    WHERE GameId = {game_id_to_filter}
);

10-07 17:48