如何将常见列(A,B)和(A,C)的2个查询转换为一个(A,B,C)?

dev*_*ium 3 sql database oracle join

我目前有2个返回的查询

PRODUCER                       FirstQueryColumn       
------------------------------ ---------------------- 
aaaaaaaaaaaa                   1                      
bbbbbbbbbbb                    1                      

PRODUCER                       SecondQueryColumn      
------------------------------ ---------------------- 
aaaaaaaaaaaa                   2                      
bbbbbbbbbbb                    1                      
Run Code Online (Sandbox Code Playgroud)

我想知道的是我应该如何制作它以便我可以在一个查询中获得相同的数据,也就是说,我想要的东西会产生(Producer, FirstQueryColumn, SecondQueryColumn).

我怎样才能做到这一点?

这是我目前的查询:

select Producers.name Prod, count(Animals.idanimal) AnimalsBought
from AnimalsBought, Animals, Producers
where (AnimalsBought.idanimal = Animals.idanimal) and (Animals.owner = Producers.nif) group by Producers.name;

select Producers.name Prod, count(Animals.idanimal) AnimalsExploration
from AnimalsExploration, Animals, Producers
where (AnimalsExploration.idanimal = Animals.idanimal) and (Animals.owner = Producers.nif) group by Producers.name;
Run Code Online (Sandbox Code Playgroud)

如您所见,对于这种情况,连接不会做太多:

select Producers.name Prod, count(AnimalsBought.idanimal) AnimalsBought, count(AnimalsExploration.idanimal) AnimalsExploration
from Producers, Animals, AnimalsBought, AnimalsExploration
where (AnimalsExploration.idanimal = Animals.idanimal) and (Animals.owner = Producers.nif) group by Producers.name;
Run Code Online (Sandbox Code Playgroud)

或者我做错了什么?

Ton*_*ews 5

首先想象2个查询只是表格.你会这样做:

select a.producer, a.firstquerycolumn, b.secondquerycolumn
from table1 a
join table2 b on b.producer = a.producer
Run Code Online (Sandbox Code Playgroud)

您可以通过查询替换每个表(称为内联视图):

select a.Prod, a.AnimalsBought, b.AnimalsExploration
from
( select Producers.name Prod, count(Animals.idanimal) AnimalsBought
  from AnimalsBought, Animals, Producers
  where (AnimalsBought.idanimal = Animals.idanimal) 
  and (Animals.owner = Producers.nif) 
  group by Producers.name
) a
join
( select Producers.name Prod, count(Animals.idanimal) AnimalsExploration
  from AnimalsExploration, Animals, Producers
  where (AnimalsExploration.idanimal = Animals.idanimal) 
  and (Animals.owner = Producers.nif)
  group by Producers.name
) b
on a.Prod = b.Prod;
Run Code Online (Sandbox Code Playgroud)

如果一个查询可能返回生产者的数据而另一个查询不返回,则可能需要将"join"更改为"full outer join".我也倾向于重构查询,如下所示,使生成器外部的主查询连接到2个子查询(删除了生成器):

select Producers.name Prod, a.AnimalsBought, b.AnimalsExploration
from Producers
left outer join ( select Animals.owner, count(AnimalsBought.idanimal) AnimalsBought
                    from AnimalsBought, Animals
                   where AnimalsBought.idanimal = Animals.idanimal
                   group by Animals.owner
                ) a
           on a.owner = Producers.nif
left outer join ( select Animals.owner, count(Animals.idanimal) AnimalsExploration
                    from AnimalsExploration, Animals
                   where AnimalsExploration.idanimal = Animals.idanimal
                   group by Animals.owner
                ) b
           on b.owner = Producers.nif;
Run Code Online (Sandbox Code Playgroud)

(正是这种类型的查询我测试了下面的性能).


不是用OP可能不感兴趣的信息来解释这个答案,我对Oracle中标量子查询和内联视图的相对性能的说明(由PerformanceDBA请求)现在在这里是脱机的:性能说明