Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.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 如何在Oracle11g中使用左外联接或右外联接_Sql_Oracle11g - Fatal编程技术网

Sql 如何在Oracle11g中使用左外联接或右外联接

Sql 如何在Oracle11g中使用左外联接或右外联接,sql,oracle11g,Sql,Oracle11g,我在where子句中有一个使用“=”的查询,但是当有很多数据时,执行该查询需要很长时间。如何使用左外连接或右外连接或类似的方法来提高性能 这是一个问题: select sum(op.quantity * op.unit_amount) into paid_money from tableA op , tableB ssl, tableC ss, tableD pl, tableE p where (op.id = ssl.id and ssL.id = ss.id and ss.type='A

我在where子句中有一个使用“=”的查询,但是当有很多数据时,执行该查询需要很长时间。如何使用左外连接或右外连接或类似的方法来提高性能 这是一个问题:

select sum(op.quantity * op.unit_amount) into paid_money
from tableA op , tableB ssl, tableC ss, tableD pl, tableE p
where  (op.id = ssl.id and ssL.id = ss.id and ss.type='A') 
    or 
    (op.id = pl.id and pl.id = p.id and p.type='B');

你的问题不是左或右连接。这是交叉连接。你正在做许多不必要的笛卡尔积。我猜这个查询永远不会结束。如果是这样的话,你肯定会得到错误的答案

将其拆分为两个单独的联接,然后将结果合并在一起。仅使用每组联接所需的表:

select SUM(val) into paid_money
from (select sum(op.quantity * op.unit_amount) as val
      from tableA op , tableB ssl
      where  (op.id = ssl.id and ssL.id = ss.id and ss.type='A')  
      union all
      select sum(op.quantity * op.unit_amount) as val
      from tableA op , tableD pl, tableD p 
      where (op.id = pl.id and pl.id = p.id and p.type='B')
     ) t

我还没有修正你的连接语法。但是,您应该学会使用
join
关键字,并将连接条件放在
on
子句中,而不是放在
where
子句中。

您确定此查询返回所需的数据吗?在我看来,它将返回每个op、pl、p匹配的op、ssl和ss的笛卡尔乘积,反之亦然


我建议您将其拆分为两个独立的查询,将它们合并在一起,然后在顶部求和

到目前为止,您在尝试包含联接时尝试了什么?你卡在哪里了?这里有部分笛卡尔连接。请发布您的表格结构、样本数据和预期结果。谢谢。你的回答给了我一个想法,我将尝试加入keywork