不使用 oracle minus 获取不匹配的记录 except not in
Get unmatched records without using oracle minus except not in
实际上我有两个 table 并且每个都有列名,我只想要 Table2
中没有的结果
Table1
----
Name
---
|A|
|B|
|C|
|D|
Table2
------
|Name|
-----
|A|
|B|
回答
|C|
|D|
我可以用减号
select name from table1
minus
select name from table2
select name from table1 where name
not in (
select name from table2)
但是我的经理要求我使用其他替代解决方案来完成,而不使用 minus,except,not in。
有没有办法做到这一点,如果有人可以帮助我,那就太好了。
我需要用 oracle pl/sql
您剩下的一个选择是使用 NOT EXISTS
SELECT t1.name
FROM table1 t1
WHERE NOT EXISTS (SELECT 'X'
FROM table2 t2
WHERE t2.name = t1.name);
更新:使用加入
with table_ as
(
select t1.name t1_name, t2.name t2_name
from table1 t1
left join table2 t2
on t1.name = t2.name)
select t1_name
from table_
where t2_name is null;
或者只是
select t1.name
from table1 t1
left join table2 t2
on t1.name = t2.name
where t2.name is null;
另一种方法是使用外连接,然后过滤第二个没有值的行 table:
with t1 as (select 'A' name from dual union all
select 'B' name from dual union all
select 'C' name from dual union all
select 'D' name from dual),
t2 as (select 'A' name from dual union all
select 'B' name from dual)
select t1.name
from t1
left outer join t2 on (t1.name = t2.name)
where t2.name is null;
NAME
----
D
C
实际上我有两个 table 并且每个都有列名,我只想要 Table2
中没有的结果Table1
----
Name
---
|A|
|B|
|C|
|D|
Table2
------
|Name|
-----
|A|
|B|
回答 |C| |D|
我可以用减号
select name from table1
minus
select name from table2
select name from table1 where name
not in (
select name from table2)
但是我的经理要求我使用其他替代解决方案来完成,而不使用 minus,except,not in。 有没有办法做到这一点,如果有人可以帮助我,那就太好了。 我需要用 oracle pl/sql
您剩下的一个选择是使用 NOT EXISTS
SELECT t1.name
FROM table1 t1
WHERE NOT EXISTS (SELECT 'X'
FROM table2 t2
WHERE t2.name = t1.name);
更新:使用加入
with table_ as
(
select t1.name t1_name, t2.name t2_name
from table1 t1
left join table2 t2
on t1.name = t2.name)
select t1_name
from table_
where t2_name is null;
或者只是
select t1.name
from table1 t1
left join table2 t2
on t1.name = t2.name
where t2.name is null;
另一种方法是使用外连接,然后过滤第二个没有值的行 table:
with t1 as (select 'A' name from dual union all
select 'B' name from dual union all
select 'C' name from dual union all
select 'D' name from dual),
t2 as (select 'A' name from dual union all
select 'B' name from dual)
select t1.name
from t1
left outer join t2 on (t1.name = t2.name)
where t2.name is null;
NAME
----
D
C