Oracle选择子查询

时间:2017-07-26 21:19:35

标签: sql oracle select

我有两个oracle选择查询,如

 SELECT req.requisition AS req, exp.experiment AS expt 
 FROM experiment_view exp, requisition_view req, association_view ass 
 WHERE expt.name = 'RETRIEVAL'AND req.name = 'TRANSFER' 
 AND ass.entity_id_2 = req.entity_id AND ass.entity_id_1 = expt.entity_id 

结果如下所示:

enter image description here

其他查询如下:

SELECT

结果

enter image description here

我正在尝试将这两个AllowParseableTypes查询结合起来,以便我看到这些结果:

enter image description here

我应该使用子查询来查看组合结果还是有其他优化方法?

3 个答案:

答案 0 :(得分:1)

我不确定提供的解决方案是否正确。所有这些都使用了1个连接到关联表。您需要2.因为Association看起来是一个通用映射表,因此将位置连接到请购单的行与将请购单连接到实验的行不同。也许我错了,但我会选择:

SELECT
  loc.location as LOCATION , 
  req.requisition as REQ, 
  exp.experiment as EXPT
FROM  location_view    loc
JOIN  association      asslr ON asslr.entity_id_1 = loc.entity_id
JOIN  requisition_view req   ON asslr.entity_id_2 = req.entity_id and req.name = 'TRANSFER'
JOIN  association_view assre ON assre.entity_id_2 = req.entity_id
JOIN  experiment_view  exp   ON assre.entity_id_1 = exp.entity_id AND exp.name = 'RETRIEVAL'
WHERE loc.name = 'ABC' 

答案 1 :(得分:0)

两个查询几乎相同。结果可以在一个共同元素上连接在一起,所以是的,它们只能写成一个查询:

select loc.location as LOCATION , req.requisition as REQ, exp.experiment as expt
from location_view loc, requisition_view req, association ass, experiment_view exp
where  loc.name = 'ABC' and req.name = 'TRANSFER' and ass.entity_id_2 = req.entity_id and ass.entity_id_1 = loc.entity_id and ass.entity_id_1 = expt.entity_id and expt.name = 'RETRIEVAL'

这是编写查询的一种古老的非标准方式;了解INNER JOIN关键字的工作原理;在这里我是如何解决这个问题的:

select
  loc.location as LOCATION , 
  req.requisition as REQ, 
  exp.experiment as expt
from 
  association ass
  INNER JOIN
  location_view loc
  ON
    ass.entity_id_1 = loc.entity_id

  INNER JOIN 
  requisition_view req 
  on 
    ass.entity_id_2 = req.entity_id

  INNER JOIN
  experiment_view expt
  ON
    ass.entity_id_1 = expt.entity_id

WHERE        
  loc.name = 'ABC' and
  req.name = 'TRANSFER' and
  expt.name = 'RETRIEVAL'

答案 2 :(得分:0)

SELECT loc.location AS location, 
       req.requisition AS req,
       exp.experiment AS expt
  FROM location_view loc
 INNER JOIN association ass
    ON loc.entity_id = ass.entity_id_1
 INNER JOIN requisition_view req
    ON req.entity_id = ass.entity_id_2
 INNER JOIN experiment_view exp
    ON expt.entity_id = ass.entity_id_1
 WHERE loc.name = 'ABC'
   AND req.name = 'TRANSFER' 
   AND expt.name = 'RETRIEVAL'