选择连接两个表时不存在的行

时间:2015-09-09 07:45:52

标签: sql oracle join oracle11g

我有两个表可以使用一个字段连接:

Table_1: 

    emp_id  emp_name  department
    ------  --------  ----------
    1       Adam      Accounting
    2       Peter     Engineering
    3       Bruce     Engineering


Table_2: 

    emp_id  emp_salary
    ------  ----------
    1       1000 
    3       3500
    5       2000

我想选择表2中连接两个表时未出现的行(在此示例中为emp_id = 5)。我一直在尝试以下语句,但我得到0行:

select * from table_2 
where not exists
(
select * from table_1, table_2
where table_1.emp_id = table_2.emp_id);

3 个答案:

答案 0 :(得分:2)

这么简单,只需从子查询中删除table_2:

select *
from table_2
where not exists (select 1
                  from table_1
                  where table_1.emp_id = table_2.emp_id);

答案 1 :(得分:2)

尝试:

select * from table_2 
where
emp_id not in (select emp_id from table_1)

答案 2 :(得分:0)

从table_2中选择* 哪里 emp_id not in(从table_1中选择emp_id)

如果emp_id在表中都是主要的,则可以使用此查询。

相关问题