Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 从第一个表中获取行,即使它们不存在于联接中的最后一个表中,但不进行左联接_Sql_Oracle - Fatal编程技术网

Sql 从第一个表中获取行,即使它们不存在于联接中的最后一个表中,但不进行左联接

Sql 从第一个表中获取行,即使它们不存在于联接中的最后一个表中,但不进行左联接,sql,oracle,Sql,Oracle,我正在使用一个Oracle数据库,我有一个查询,其中我必须执行7个不同表的联接。 现在,我的问题是,我需要得到满足连接要求的行,但即使它们不符合上一次连接的条件,我也需要从前6个表中得到行。 我不能做左外连接,那么我有什么选择呢 代码如下所示: with tmp as (select col1, col2, col3, col4, row_number() over (partition by col1 order by col2 desc) rn from

我正在使用一个Oracle数据库,我有一个查询,其中我必须执行7个不同表的联接。 现在,我的问题是,我需要得到满足连接要求的行,但即使它们不符合上一次连接的条件,我也需要从前6个表中得到行。 我不能做左外连接,那么我有什么选择呢

代码如下所示:

    with
tmp as (select col1, col2, col3, col4, row_number() over (partition by col1 order by col2 desc) rn
            from
            (select /*+ MATERIALIZE */
              col1, col2, col3, col4
             from
              table1
             where 
              col3 in ('A','R','F') and
              somedate >= sysdate-720 and
              col5 is null
              and col1<> '0000000000'))
select /*+ use_hash(a,b,c,d,e,f,g,h) */
b.col5,
a.col1, 
 d.col6, 
 e.col7, 
 c.col8 , 
 (CASE when f.col9= 'B' then 'Foo' else 'Bar' END) as "col9", 
 a.col2, 
 a.col3,
 h.col10
from tmp a
     join table2 b on
      a.col1= b.col1 and
      a.col4=b.col4 and
      b.col11='P' and
      (b.otherDate>= sysdate OR b.otherDate is null)  and
      b.col5 is null 
     join table3 c on
      b.col12 = c.col12 and
      (c.otherDate is null or b.otherDate >= sysdate) and
      c.col5 is null 
     join table4 d on
      a.col1= d.col1 and
      d.col13 in ('R','A','F') and
      d.col5 is null 
     join table5 e on
      e.col1=b.col1 and
      e.col14=d.col14 and
      d.col6=e.col6 and
      d.col15 = e.col15 and
      e.col5 is null       
    join table6 f on
      f.col4= a.col4 and 
      f.col5 is null
    join table7 g on 
      g.col16=  case when f.col15 is null then null else f.col15 end
      and g.col5is null
      and (g.otherDate is null or g.otherDate >= sysdate)
    join table8 h on 
      h.col17= g.col17
      and (h.otherDate >= sysdate or h.otherDate is null)
      and h.col5 is null
      and a.rn=1;

我不会尝试处理您的实际查询,但原则上您可以更改:

select tab1.col1, tab2.col2, tab3.col3
from tab1
join tab2 on tab2.fk = tab1.pk
join tab3 on tab3.fk = tab2.pk
进入:

您可以用以下内容替换您的out joins not allowed world:

with tmp as (
    select tab1.col1, tab2.col2, tab3.pk
    from tab1
    join tab2 on tab2.fk = tab1.pk
)
select tmp.col1, tmp.col2, tab3.col3
from tmp
join tab3 on tab3.fk = tmp.pk
union all
select tmp.col1, tmp.col2, null as col3
from tmp
where not exists (
    select null from tab3
    where tab3.fk = tmp.pk
)
这是相当丑陋的-我已经尽量减少了重复与一个CTE,但即使如此不好-并可能表现不好,以及外部连接将


当然,在不知道为什么不能使用外部联接的情况下,我不知道是否还有其他限制也会使这种方法无法接受……

使用左联接代替联接为什么不能使用外部联接?另一种方法是对同一个查询进行多次合并,但使用not exists而不是最后一个联接,这很难看。我不能使用外部联接,因为Oracle抛出了ORA-01719异常,这显然是因为我使用了分析函数。但我想你有我需要的答案,干杯!
with tmp as (
    select tab1.col1, tab2.col2, tab3.pk
    from tab1
    join tab2 on tab2.fk = tab1.pk
)
select tmp.col1, tmp.col2, tab3.col3
from tmp
join tab3 on tab3.fk = tmp.pk
union all
select tmp.col1, tmp.col2, null as col3
from tmp
where not exists (
    select null from tab3
    where tab3.fk = tmp.pk
)