简体   繁体   English

sql加入同一个表但不同的列

[英]sql Joining on same table but different column

Contacts联系人

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

tFormat格式

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

Expected results预期成绩

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

Current results当前结果

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

The query displays telephone numbers and operator for t1 but only displays the telephone number and not the operator for t2.查询显示 t1 的电话号码和运营商,但只显示电话号码,而不显示 t2 的运营商。 There is no relation between the two tables两个表之间没有关系

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

Below are the results for the test data which you provided, query is same as yours and I have added NVL clause in join condition as there were null telephone numbers.以下是您提供的测试数据的结果,查询与您的相同,我在连接条件中添加了 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.

I think you want:我想你想要:

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 is a db<>fiddle. 是一个 db<>fiddle。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM