table A
id city
1 koronadal
2 cebu
3 manila
Table B
id city
1 cebu
Expected ouput:
id city
1 koronadal
3 manila
Here is my query:
Select a.id, a.city from tablea a left join tableb b on a.city = b.city
I get the wrong output.. please help me..
You are on the right way! now you just need to filter those who match :
Select a.id, a.city
from tablea a
left join tableb b
on a.city = b.city
WHERE b.id is null
In LEFT JOINING
when there is no match of the join condition, the left table, in this case tableA will still be selected, and all the data from the right table, in this case tableB will be NULL
. So all you need to do is search for null values on B table.
Another approach would be to use IN()
:
SELECT * FROM TableA a
WHERE a.city NOT IN(SELECT city FROM TableB)
And another way is EXISTS()
:
SELECT * FROM TableA a
WHERE NOT EXISTS(SELECT 1 FROM TableB b
WHERE a.city = b.city)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.