从'table2'中删除其中column ='value'IF'table1'中的列='value'

时间:2013-07-10 23:41:07

标签: mysql where sql-delete

我正在尝试执行MySQL查询以从'table2'中删除行,其中column ='value''table1'中的IF列='value'

我有2张桌子......

表1称为“账户” 表2称为“inventoryitems”

“帐户”中的相关列称为“禁止” “inventoryitems”的问题列称为“itemid”

我想删除inventoryitems WHERE itemid = 2340000 如果... banned中的accounts列的值为1

额外信息:

您可以通过名为accounts的第3个表格将表格inventoryitems加入characters

accounts包含列:id(主键)和banned

characters包含以下列:characteridaccountidaccountid指向表idaccounts的链接。)

inventoryitemsitemidcharacterid characterid指向表characterid中的characters

希望这有帮助。

3 个答案:

答案 0 :(得分:1)

DELETE FROM inventoryitems WHERE characterid IN (SELECT id from characters WHERE accountid IN (SELECT id from accounts WHERE banned = '1' ) ) AND itemid = '2340000';

答案 1 :(得分:0)

以下查询使用INNER JOIN从表inventoryitems中删除一行:

DELETE i FROM inventoryitems i  
INNER JOIN characters c ON i.characterid = c.characterid 
INNER JOIN accounts a ON c.accountid = a.id 
WHERE i.itemid = 2340000 && a.banned = 1;

答案 2 :(得分:0)

试试这个,因为我从你的评论中收集到他们可能会以这种方式加入。

DELETE FROM inventoryitems WHERE characterid IN
(SELECT characterid from characters WHERE accountid IN
(SELECT id from accounts WHERE banned = 1))
AND itemid = '2340000';