Delete unmatching records

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?
 
P

Paolo

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
 
H

Henk

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
 
D

Douglas J. Steele

Why not just

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top