简体   繁体   English

Oracle选择带子查询

[英]Oracle select with sub queries

I have two oracle select queries like 我有两个甲骨文选择查询

SELECT loc.location AS LOCATION , req.requisition AS REQ 
FROM location_view loc, requisition_view req, association ass 
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 the result looks like: 结果看起来像:

在此处输入图片说明

Other query is like: 其他查询如下:

 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 

Result : 结果

在此处输入图片说明

I am trying to combine these two SELECT queries so I get to see these results: 我试图结合这两个SELECT查询,以便我看到这些结果:

在此处输入图片说明

Should I be using Sub-Queries to see the combined result or is there any other way of optimizing? 我应该使用子查询来查看组合结果还是有其他优化方法?

I'm not sure the provided solutions are correct. 我不确定所提供的解决方案是否正确。 All of them are using 1 join to the association table. 他们都使用1连接到关联表。 You need 2. Because Association looks to be a generic mapping table so the row that joins locations to requisitions is not the same as the one which joins requisitions to experiments. 您需要2。因为关联看上去是一个通用的映射表,所以将位置链接到申请的行与将请求链接到实验的行不同。 Maybe i'm wrong but i'd go for : 也许我错了,但我会去:

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' 

The two queries are nearly identical. 这两个查询几乎相同。 The results can be joined together on a common element so yes, they can just be written as one query: 结果可以在一个公共元素上连接在一起,因此,可以将它们编写为一个查询:

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'

This is a bit of an ancient nonstandard way to write queries though; 不过,这是一种古老的非标准方式来编写查询; look into how the INNER JOIN keywords work; 研究INNER JOIN关键字的工作方式; here's how I'd have laid this query out: 这是我如何安排此查询的方式:

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'
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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM