Filter values in table X from table Y
A simplified example of what i'm trying to achieve:
Table 1
FirstName | LastName | Age | Eye Color |
---------------------------------------|
John | Dunbar | 30 | Blue |
Jane | Austin | 98 | Green |
John | Dunbar | 21 | Red |
John | Dunbar | 23 | Brown |
Mr | T | ... |
One | More | ... |
Table 2
FirstName开发者_如何学JAVA | LastName |
---------------------|
John | Dunbar |
Mr | T |
So, What I would like to create is Table 1 without all records matching table 2. In other words, Table 3:
FirstName | LastName | Age | Eye Color |
---------------------------------------|
Jane | Austin | 98 | Green |
One | More | ... |
I'm not sure what's the best "select" to get there. I'm guessing some smart usage of "join", but not sure...
Table3 - select rows from Table1 which does not present in Table2
CREATE TABLE Table3
SELECT t1.* from Table1 t1
WHERE NOT EXISTS(Select 1 from Table2 t2
WHERE t1.FirstName = t2.FirstName
AND t1.LastName = t2.LastName)
Table3 - select rows from Table1 which present in Table2
CREATE TABLE Table3
SELECT t1.* from Table1 t1
WHERE EXISTS(Select 1 from Table2 t2
WHERE t1.FirstName = t2.FirstName
AND t1.LastName = t2.LastName)
SELECT * from Table1 where FirstName not in (Select Firstname from Table2) and LastName not in (Select LastName from Table2)
This should work
精彩评论