Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.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,我的SQL没有它可能的那么强大。与写C#相比,这是一种不同的思维方式。我正在处理一个查询,并使用“left join/is null”重写它,以摆脱“not in”语法 有没有办法避免写两次ratherLargeSubquery?我试过了 select t1.id, t2.col1 from table1 t1 join table2 t2 on t1.id = t2.id left join (select sub.id from ratherLargeSubquery sub) s1

我的SQL没有它可能的那么强大。与写C#相比,这是一种不同的思维方式。我正在处理一个查询,并使用“left join/is null”重写它,以摆脱“not in”语法

有没有办法避免写两次ratherLargeSubquery?我试过了

select t1.id, t2.col1
from table1 t1
join table2 t2 on t1.id = t2.id
left join (select sub.id from ratherLargeSubquery sub) s1
   On (s1.id = t1.id and t2.id = s1.id)
where
s1.id is null
但当然,这会返回相同的结果,就像我从未在第一个位置添加左连接一样

select t1.id, t2.col1
from table1 t1
join table2 t2 on t1.id = t2.id
left join (select sub.id from ratherLargeSubquery sub) s1
   On (s1.id = t1.id or s1.id = t2.id)
where
s1.id is null
只运行超过原始查询时间长度20倍的时间,而不返回任何结果


回到问题,有没有办法不写两次ratherLargeSubquery就写它呢?因为您没有从子查询中选择任何列,所以可以将筛选移到
where
子句

select t1.id, t2.col1
from table1 t1 join
     table2 t2
     on t1.id = t2.id 
where not exists (select 1
                  from ratherLargeSubquery sub
                  where sub.id = t1.id or sub.id = t2.id
                 );

这简化了查询的编写。但是,Oracle可能无法对其进行优化。

因为您没有从子查询中选择任何列,所以可以将筛选移到
where
子句

select t1.id, t2.col1
from table1 t1 join
     table2 t2
     on t1.id = t2.id 
where not exists (select 1
                  from ratherLargeSubquery sub
                  where sub.id = t1.id or sub.id = t2.id
                 );

这简化了查询的编写。但是,Oracle可能无法优化它。

看来SQL实际上不需要左二联接

因为t1.id=t2.id
那么s1.id=s2.id

select t1.id, t2.col1
from table1 t1
join table2 t2 on t1.id = t2.id
left join (select id from ratherLargeSubquery) s1
   on s1.id = t1.id
where s1.id is null 

看起来SQL实际上不需要第二个左连接

因为t1.id=t2.id
那么s1.id=s2.id

select t1.id, t2.col1
from table1 t1
join table2 t2 on t1.id = t2.id
left join (select id from ratherLargeSubquery) s1
   on s1.id = t1.id
where s1.id is null 
使用(t1.id,t2.id)中的s.id作为连接条件:

select t1.id, t2.col1
from table1 t1
join table2 t2 on t1.id = t2.id
left join (select sub.id from ratherLargeSubquery sub) s
   on s.id in (t1.id, t2.id)
where s.id is null
使用(t1.id,t2.id)中的s.id作为连接条件:

select t1.id, t2.col1
from table1 t1
join table2 t2 on t1.id = t2.id
left join (select sub.id from ratherLargeSubquery sub) s
   on s.id in (t1.id, t2.id)
where s.id is null

我几乎不愿意发表这篇文章,因为许多有见识的海报都没有提到它,但为什么不用一个常见的表格表达呢

我想你真正的反对不是有两个左连接,而是必须重复“ratherLargeSubquery”的逻辑


我几乎不愿意发表这篇文章,因为许多有见识的海报都没有提到它,但为什么不用一个常见的表格表达呢

我想你真正的反对不是有两个左连接,而是必须重复“ratherLargeSubquery”的逻辑


你能提供一些样本数据和预期结果吗?你能提供一些样本数据和预期结果吗?不要犹豫。就这么做吧--不要犹豫。就这么做吧--耐克