How to delete rows using delete and select mysql
I use this rows not work :
delete from items T1 where T1.base_item =
(SELECT T2.type,T2.id From furniture T2 where T2.type = 'i' );
How to delete rows using delete and select mysql
I use this rows not work :
delete from items T1 where T1.base_item =
(SELECT T2.type,T2.id From furniture T2 where T2.type = 'i' );
This does not work because = expects a single value while the subselect returns a result set.
There are two approaches you can take, you can use an IN() clause or your can delete across a join.
The first would look like this:
DELETE FROM items
WHERE base_item IN (SELECT id FROM furniture WHERE type = 'i')
Note that only a single field should be specified in the subselect. I am assuming that furniture.id relates to items.base_item in this case. If furniture.type is really the matching field, use that in the subselect instead.
The second approach would look like this:
DELETE items
FROM items INNER JOIN furniture
ON items.base_item = furniture.id
WHERE furniture.type = 'i'
Again my assumption here is that items.base_item relates to furniture.id.
Which is better to use (assuming all applicable fields are indexed) would likely depend on the number of rows expected in each table and the cardinality of your data. You might want to test yourself to see which is more efficient for your use case.