【发布时间】:2021-10-04 08:33:00
【问题描述】:
我有一个左外连接,它不会返回“左”表中的所有行。我没有 where 子句,所以在 join 之后不应该应用过滤。
我期待:
| Product 1 | AT | 100 |
| Product 2 | AT | 25 |
| Product 4 | AT | 57 |
| Product 1 | GR | 45 |
| Product 2 | GR | 22 |
| Product 3 | GR | 5 |
| Product 4 | GR | 4 |
| Product 3 | null | null |
但我错过了最后一行。非常感谢您对此提出的任何启示。
复制它:
-- Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
drop table t1;
drop table t2;
create table t1
(ov_product varchar2(18 byte)
,product varchar2(18 byte)
)
/
create table t2
(reporting_month number
,product varchar2(18 byte)
,sender varchar2(2 byte)
,items number
)
/
insert into t1
(
select 'Product 1' ov_product, 'P1' product from dual
union
select 'Product 2' ov_product, 'P2' product from dual
union
select 'Product 3' ov_product, 'P3' product from dual
union
select 'Product 4' ov_product, 'P4' product from dual
);
insert into t2
(
select 202108, 'P1', 'AT', 100 from dual
union
select 202108, 'P2', 'AT', 25 from dual
union
-- no P3 for AT
select 202108, 'P4', 'AT', 57 from dual
union
select 202108, 'P1', 'GR', 45 from dual
union
select 202108, 'P2', 'GR', 22 from dual
union
select 202108, 'P3', 'GR', 5 from dual
union
select 202108, 'P4', 'GR', 4 from dual
)
;
commit;
select t1.ov_product
,t2.sender
,t2.items
from t1
left outer join t2
on t1.product = t2.product
order by 2, 1
;
【问题讨论】: