0

我在 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}";`
4

3 回答 3

3

如果在您的情况下使用 anINNER JOIN不是那么重要,您还可以使用以下内容:

DELETE FROM EncounterCards
WHERE EncounterId IN (
    SELECT id FROM Encounters
    WHERE GameId = {game_id_to_filter}
);
于 2012-12-24T05:47:59.637 回答
0

试试这个查询:

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

在 FROM 关键字之前添加了表名。请参阅多表删除语法

于 2012-12-24T05:43:02.080 回答
0

您需要指定应删除记录的位置。在这种情况下,我在关键字ALIAS之前添加了一个。FROM

DELETE  a
FROM    EncounterCards a 
        INNER JOIN Encounters b
            ON a.EncounterID = b.ID
WHERE   b.GameID = 5
于 2012-12-24T05:43:12.873 回答