I have two tables
Table A :
Item Lookup
A X
B null
C Y
D K
Table B :
Lookup
X
Y
Z
I want to join these tables and get output like
Item Lookup
A X
B null
C Y
我想在输出视图中选择所有匹配的查找和空查找。任何人都可以告诉我加入条件
答案 0 :(得分:4)
您希望B
中的行与NULL
匹配或inner join
查找。您可以使用select distinct a.item, a.lookup
from tableA a join
tableB b
on (a.lookup = b.lookup) or (a.lookup is NULL);
和特殊条件执行此操作:
lookup
如果您有exists
的索引,那么select a.item, a.lookup
from tableA a
where a.lookup is null or
exists (select 1 from tableB b where b.lookup = a.lookup);
的效果会更好:
left join
编辑:
也可以使用where
select distinct a.item, a.lookup
from tableA a left join
tableB b
on a.lookup = b.lookup
where (a.lookup is NULL and b.lookup is NULL) or
b.lookup is not null;
条件:
<MY_JENKINS_INSTANCE>/git/notifyCommit?url=$CHANGED_REPOSITORY
答案 1 :(得分:3)
以下查询将返回Lookup
中TableB
的{{1}}的所有记录,或NULL
Lookup
的所有记录:
SELECT Item, Lookup
FROM TableA
WHERE (Lookup IS NULL) OR Lookup IN (SELECT Lookup FROM TableB)
答案 2 :(得分:1)
我认为这会有所帮助
SELECT DISTINCT A.Item, A.Lookup FROM A, B WHERE A.lookup=B.lookup OR A.lookup IS NULL
答案 3 :(得分:0)
您也可以通过左外连接来执行此操作:
with tablea as (select 'A' item, 'X' lookup from dual union all
select 'B' item, null lookup from dual union all
select 'C' item, 'Y' lookup from dual union all
select 'D' item, 'K' lookup from dual),
tableb as (select 'X' lookup from dual union all
select 'Y' lookup from dual union all
select 'Z' lookup from dual)
select a.item,
a.lookup
from tableA a
left outer join tableB b on (a.lookup = b.lookup)
where a.lookup is null
or a.lookup = b.lookup;
ITEM LOOKUP
---- ------
A X
C Y
B