Wednesday, April 12, 2017

Return not matching rows after joining two tables

I am not sure how to ask this correctly, but here it is:

table1

id   |    email
---------------------------
1         test@test.com
---------------------------
2         random@test.com
---------------------------
3         magic@test.com
---------------------------
4         example@test.com
---------------------------
5         noreply@test.com
---------------------------
6         admin@test.com
---------------------------
7         editor@test.com

table2

id   |    email_two
---------------------------
7         test@test.com
---------------------------
10        random@test.com
---------------------------
33        magic@test.com
---------------------------
99        example@test.com
---------------------------
109       master@test.com
---------------------------
299       blaster@test.com

Question:

How to correctly join two tables, and get not matching results by email? For example, what i need to get from both tables is:

noreply@test.com
admin@test.com
editor@test.com
master@test.com
blaster@test.com

because other emails matching each other.

Code

SELECT email_two FROM table2 b WHERE NOT EXISTS (SELECT * FROM table1 a WHERE a.email = b.email_two

This code returns only missing ones from table2, but i cant find a correct way to return missing results from two tables in one query.

Thanks for any answers.



via Tauras

Advertisement