简体   繁体   中英

Delete from 'table2' where column = 'value' IF column in 'table1' = 'value'

I am trying to execute a MySQL query to delete rows from 'table2' where column = 'value' IF column in 'table1' = 'value'

I have 2 tables...

Table 1 is called 'accounts'
Table 2 is called 'inventoryitems'

The column in question for 'accounts' is called 'banned'
The column in question for 'inventoryitems' is called 'itemid'

I would like to DELETE FROM inventoryitems WHERE itemid = 2340000 IF... the column banned in accounts has a value of 1

Extra Information:

You can join the table accounts to inventoryitems by a 3rd table called characters .

Table accounts has columns: id (primary key) and banned .

Table characters has columns: characterid and accountid ( accountid links to id in table accounts ).

Table inventoryitems has columns itemid and characterid ( characterid links to characterid in table characters )

Hope this helps.

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

The following query is using a INNER JOIN to remove one row from table 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;

Try this, since I gathered from your comment that they can be probably joined this way.

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

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