I have a MySQL database with a relationship table which can be simplified to:
CREATE TABLE `Paths` (
`origin` char(32) NOT NULL,
`destination` char(32) NOT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`),
)
This is a many-to-many relationship.
I want to delete all paths where all paths from that origin only lead to a certain place, e.g. Neverland.
This SQL should work:
DELETE FROM Paths WHERE origin IN (SELECT DISTINCT origin FROM Paths WHERE destination = 'Neverland') AND origin NOT IN (SELECT DISTINCT origin FROM Paths WHERE destination <> 'Neverland');
But, is there a better, more efficient way?
Thanks!
Try: