Attempting to delete a list of id's generated by a sub-select in a single statement. Is this possible? I've been trying the following:
[edit] corrected query
DELETE from store
WHERE id in ( SELECT store.id
FROM storeInfo
JOIN store ON storeInfo.store_id = store.id
WHERE storeInfo.type = 17 )
But this is incorrect syntax ... not sure how to build this properly.
DELETE store
FROM store
JOIN storeInfo
ON storeInfo.store_id = store.id
WHERE storeInfo.type = 17
After the typo update to the question - you don't need a JOIN
in the subquery. You can just return storeInfo.store_id
from the IN()
subquery.
DELETE FROM `store` WHERE `id` IN (SELECT `store_id` FROM `storeInfo` WHERE `type` = 17)
MySQL would not permit you to use the table store
in the subquery and then delete rows from it. However, you can use storeInfo
since it joins directly with store_id
.
If you need to delete from both of these tables (such as if the rows in storeInfo
become orphaned when their parents are deleted from store
), use the JOIN
syntax instead:
DELETE
/* Syntax to delete rows from both tables */
store, storeInfo
FROM
store JOIN storeInfo ON store.id = storeInfo.store_id
WHERE storeInfo.type = 17
Review the MySQL DELETE
syntax reference for full details.
You can do the join directly into the delete
delete store from store
JOIN storeInfo ON storeInfo.store_id = store.id
WHERE store.type = 17;
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.