【问题标题】:sql Joining on same table but different columnsql加入同一张表但不同的列
【发布时间】:2019-02-08 06:27:43
【问题描述】:

联系人

telephone1     telephone2
---------------------------
+271566681     NULL
+276445884     +271679161
+275684835     NULL
NULL           +276496136

tFormat

operator     range
-------------------
MTN          +2764
Vodacom      +2716

预期结果

TELEPHONE1     OPERATOR     TELEPHONE2     OPERATOR
---------------------------------------------------
+271666681     Vodacom      NULL           NULL
++276445884    MTN          +271679161     Vodacom
 NULL          NULL         +276496136     MTN

当前结果

TELEPHONE1     OPERATOR     TELEPHONE2     OPERATOR
---------------------------------------------------
+271666681     Vodacom      NULL           NULL
+276445884     MTN          +271679161     NULL
 NULL          NULL         +276496136     NULL

查询显示 t1 的电话号码和运营商,但仅显示电话号码而不显示 t2 的运营商。两个表之间没有关系

select    
    c.telephon1, t1.operator
    c.telephone2, t2.operator
from
    Contacts as c 
left join 
    tFormat as t1 on left(c.telephone1, 5) = t1.range
left join 
    tFormat as t2 on left(c.telephone2, 5) = t2.NUMBER_RANGE

【问题讨论】:

  • 请在您的问题中添加示例数据。您的联接逻辑似乎表明这两个表之间存在关系。
  • 表示 t2 与 t1 未正确连接(连接逻辑错误)。请提供Minimal, Complete, and Verifiable example
  • 您当前的查询给出了您的预期结果,我试过了,

标签: sql join trim


【解决方案1】:

以下是您提供的测试数据的结果,查询与您的相同,我在连接条件中添加了 NVL 子句,因为电话号码为空。

with t1 as
(
select '+271566681' as telephone1, null as telephone2 from dual
union
select '+276445884' as telephone1, '+271679161' as telephone2 from dual
union
select '+275684835' as telephone1, NULL as telephone2 from dual
union
select NULL as telephone1, '+276496136' as telephone2 from dual
)
,t2 as 
(
select 'MTN' as opetr, '+2764' as rnge from dual
union
select 'Vodacom' as opetr, '+2716' as rnge from dual
)

select
t1.telephone1, t22.opetr,
t1.telephone2, t23.opetr
from t1 
left outer join t2 t22 on substr(nvl(t1.telephone1, '00000'),1,5) = t22.rnge
left outer join t2 t23 on substr(nvl(t1.telephone2, '00000'),1,5) = t23.rnge;

NULL    NULL    +276496136  MTN
+276445884  MTN +271679161  Vodacom
+271566681  NULL    NULL    NULL
+275684835  NULL    NULL    NULL

Your query would be -
    select
    t1.telephone1, t22.operator,
    t1.telephone2, t23.operator
    from Contacts t1 
    left outer join tFormat t22 on substr(nvl(t1.telephone1, '00000'),1,5) = t22.range
    left outer join tFormat t23 on substr(nvl(t1.telephone2, '00000'),1,5) = t23.range;

Note - There are issue with the test data which you have provided
> Table has 4 records but output has 3 records
> we don't have telephone1 number starting with +2716, but your output has one
> There is record in output which starts with ++, which is not there in your test data.

【讨论】:

  • 有数以千计的数字。这条路很长,没有更短的路吗
  • 因为我不想在 db 中创建这些表,所以我使用了 CTE 并对您给出的值进行硬编码。您不必这样做。
  • 也添加了查询,如果答案有帮助,如果您可以投票将很有帮助
【解决方案2】:

我想你想要:

select c.telephone1, t1.operator,
       c.telephone2, t2.operator
from Contacts  c left join 
     tFormat t1
     on left(c.telephone1, 5) = t1.range left join 
     tFormat t2
     on left(c.telephone2, 5) = t2.range
where t1.range is not null or t2.range is not null;

Here 是一个 dbfiddle。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    • 2021-06-16
    • 2018-02-04
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    相关资源
    最近更新 更多