我有2张桌子,如下所示。我需要获取2个表之间的匹配名称,以及2second表中不匹配的NULL记录。列号(键Vlaues)应该在Join中。我用下面的查询。但是,有什么方法可以摆脱Union,或者有没有比下面更好的写查询方法了?
FirstTable
Number|Name
1 |Oracle
2 |SAP
3 |IQ
4 |HANA
5 |Oracle
Second Table
Number|Name
1 |Oracle
2 |
3 |Sybase
4 |HANA
5 |
Query:
-------
select a.name from FirstTable a, Secondtable b
where a.number=b.number
and a.name=b.name
and b.name is not null
union
select a.name from FirstTable a, Secondtable b
where a.number=b.number
and b.name is null
Required Output
Name
Oracle
SAP
HANA
答案 0 :(得分:1)
对于我来说,此请求更方便
select distinct a.name from FirstTable a,SecondTable b
where a.number=b.number and a.name=nvl(b.name,a.name)
答案 1 :(得分:0)
使用join和union all,并且永远不要使用逗号分隔的连接
select coalesce(a.name,b.name) as name from
Secondtable b join FirstTable a
a.name=b.name or a.number=b.number
union all
select name from Secondtable where name is null