Help required with SQLs
I am trying to join 2 tables and trying to fetch the records, which do not have values in the join column of the 2nd table. For example: Table 1 Id column values: 1, 2, 3, 4,
Table 2 Id column values: 1, 3,
Given the above example of the values in the join columns of the 2 tables, I want to fetch records from table1 with ids 2 and 4, because they are not present in table2.
Any help would be much appreciated.
My SQL has gotten rusty to the introduction of JPA frameworks开发者_运维技巧, but today I cannot run away from not knowing it, it seems :(
Thanks!
select t1.id
from Table1 t1
left outer join Table2 t2 on t1.id = t2.id
where t2.id is null
SELECT * FROM table1 WHERE table1.id NOT IN (SELECT id from table2)
NOT EXISTS variant:
SELECT * FROM table1 WHERE NOT EXISTS
(SELECT NULL from table2 WHERE table2.id = table1.id)
精彩评论