Imagine two tables (A and B):
A        B
1        2
2        3 
6        5 
4        7
9        11
         13
         23
         9 
Now I want to delete records from A that are not present in B, e.g deleting 1, 6 and 4 from A.
My initial idea is that you could 'negate' the results of a join.
DELETE FROM A WHERE NOT EXISTS (SELECT * FROM B WHERE A.id = B.id)
I've assumed that those columns are named id.
An alternative to the NOT EXISTS answer:
DELETE FROM A WHERE id NOT IN (SELECT id FROM b);
Again, assuming the column is named id.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With