Imagine two tables (A and B):
A B 1 2 2 3 6 5 4 7 9 11 13 23 9
Now I wish to remove records from the that aren't contained in B, e.g removing 1, 6 and 4 from the.
My primary idea is that you may 'negate' the outcomes of the join.
DELETE FROM A WHERE NOT EXISTS (SELECT * FROM B ON A.id = B.id)
I have assumed that individuals posts are named
You'll find the records which are unmatched by searching for a NULL value on one for reds of the outer join, then remove individuals:
DELETE FROM A WHERE id IN ( SELECT A.id FROM A LEFT JOIN B ON A.id = B.id WHERE B.id IS NULL -- This means we only return unmatched A records )
The primary reason I love this is you can run the
SELECT area of the statement in advance to ensure that you should check your projects. Otherwise another solutions look considerably cleaner.
An alternative choice to the
NOT EXISTS answer:
DELETE FROM A WHERE id NOT IN (SELECT id FROM b);
Again, presuming the column is known as