【问题标题】:Only join two columns when it is not null in one table oracle只有在一个表oracle中不为空时才连接两列
【发布时间】:2015-12-15 06:35:03
【问题描述】:

我有一张桌子

ID | Name | City
1  |Jack  | Null
2  |Tom   | Null

还有表 b

ID | Name | City
1  |Jack  | Dever
2  |Tom   | Dallas

如果这两个表在表 a 中不为空,我需要编写一个查询来按 id、name 和 city 连接这两个表。但是这三列中的任何一列对于每一行都可能为空。

我在下面写了一个,但是当数据增长时性能很差

Select * from a, b 
Where (a.id is not null and a.id=b.id or a.id is null) and
(a.name is not null and a.name=b.name or a.name is null) and
(a.city is not null and a.city=b.city or a.city is null)

基本上,当表a中的列不为空时,我需要加入该列。 你能对此有所了解吗? 非常感谢!

【问题讨论】:

  • 您没有使用任何连接条件吗?如果你不这样做,那么你将得到笛卡尔积。因此,如果表 A 有 100 行,而表 B 有 100 行,那么您将获得不带任何 where 子句的 100*100 = 10000 行。你真的想这样做吗?你给出的查询是否得到了预期的结果?
  • 我认为这个查询和join条件是一样的,不是吗?我的期望不是笛卡尔,只是加入。或者我可以使用以下查询 Select * from a join b On (a.id is not null and a.id=b.id or a.id is null) and (a.name is not null and a.name=b .name 或 a.name 为空)和(a.city 不为空且 a.city=b.city 或 a.city 为空)
  • 也许我没有正确理解它,但请在表格中添加更多示例数据,然后在此基础上给出您的预期输出。使用sqlfiddle 创建它并分享链接,以便我们看到你做了。
  • 表达式a.id is not null and a.id=b.ida.id = b.id 相同,因为= 运算符无论如何都不会比较空值。 a.id is null 似乎表明您想要外部连接。根据您的样本数据,预期的输出是什么。请添加带有a.id = null 的示例行,并显示这些行的结果应该是什么。
  • 基本上我不想在表a中整列为空时加入该列

标签: oracle join null


【解决方案1】:

尝试我: 这会是你需要的吗?它似乎做了我可以从你的问题中读出的内容。

with a as (select 1 id, 'Jack' name, null    city from dual
            union all
            select 2 id, 'Tom'  name, null    city from dual
            union all
            select 3 id, 'Mike' name, 'Miami'   city from dual)
     ,b as (select 1 id, 'Jack' name, 'Dever'  city from dual
            union all
            select 2 id, 'Tom'  name, 'Dallas' city from dual
            union all
            select 3 id, 'Mike' name, 'Boise'   city from dual)
select b.* 
  from b
  left outer join a
    on a.id   = b.id
   and a.name = b.name
 where b.city  = nvl(a.city, b.city);

如果没有,请告知结果或数据中可能需要更改的内容。

更新一: 为了让所有列都有可能为空,这可能是一种方法。我已经为我认为您所描述的条件添加了测试数据。它给出了我认为您正在寻找的结果。

with a as (select 1 id, 'Jack' name, null    city from dual
            union all
            select 2 id, 'Tom'  name, null    city from dual
            union all
            select 3 id, 'Mike' name, 'Miami'   city from dual
            union all
            select 4 id, 'Don' name, null   city from dual
            union all
            select 5 id, null name, 'London'   city from dual
            union all
            select null id, 'Erin' name, 'Berlin'   city from dual
           )
     ,b as (select 1 id, 'Jack' name, 'Dever'  city from dual
            union all
            select 2 id, 'Tom'  name, 'Dallas' city from dual
            union all
            select 3 id, 'Mike' name, 'Boise'   city from dual
            union all
            select 4 id, 'Don' name, 'Dover'   city from dual
            union all
            select 5 id, 'Lis' name, 'London'   city from dual
            union all
            select 6 id, 'Erin' name, 'Berlin'   city from dual
           )
select b.*, a.* 
  from b
  inner join a
    on b.id   = nvl(a.id,   b.id) 
   and b.name = nvl(a.name, b.name)
   and b.city = nvl(a.city, b.city)
order by 1;

【讨论】:

  • 感谢您的回复,但是,三个整列中的任何一个都可能为空,因此当它们不为空时,我需要对列进行动态连接查询。 ps,每一行的column都为null,或者每一行的column都不为null
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-11
  • 2014-03-14
  • 1970-01-01
  • 2020-02-09
  • 1970-01-01
  • 2017-10-17
  • 2012-01-08
相关资源
最近更新 更多