Delete unmatching records

  • Thread starter Thread starter Henk
  • Start date Start date
H

Henk

Suppose I have 2 tables both containing UserId as a field. E.g :

UserId's in Table1
2
3
4
5
6

UserId's in Table2
1
2
3
4
5
6
7
8

What then is the SQL code I have to use to delete the records in Table2 that
do not match with table one?
 
Hi Henk,
here's the SQL statement you're looking for

DELETE *
FROM Table2
WHERE userid in (SELECT Table2.userid
FROM Table1 RIGHT JOIN Table2 ON Table1.userid = Table2.userid
WHERE (((Table1.userid) Is Null)))

HTH Paolo
 
tHenks !!

Paolo said:
Hi Henk,
here's the SQL statement you're looking for

DELETE *
FROM Table2
WHERE userid in (SELECT Table2.userid
FROM Table1 RIGHT JOIN Table2 ON Table1.userid = Table2.userid
WHERE (((Table1.userid) Is Null)))

HTH Paolo
 
Why not just

DELETE *
FROM Table2
WHERE userid NOT IN (SELECT DISTINCT userid
FROM Table1)
 
Back
Top