Using subqueries is inefficient; use left join with multiple table syntax :
DELETE items FROM items left join items as parent on items.parentID = parent.itemID WHERE parent.itemID is NULL
the syntax of multiple tables allows you to specify a join in the delete command, so you can join multiple tables and delete only one. That is, DELETE t1 FROM t1 join t2 .... joins t1 and t2 , but deletes only the corresponding rows from table t1 .
As for your question, βit would be nice if the elements associated with the elements deleted by this query were also deleted on request, is this possible in sql or should I encode it?β
I think this will end in cascade - a deleted record can be the parent of another record, so delete it as well, but it can also be the parent of another record, so you need to delete it too, etc. I think it is not possible to query this entire cascade with a single SQL query.
So, I think the easiest solution is to rerun the specified request until the rows are deleted.
source share