Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.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 带子查询的Oracle select_Sql_Oracle_Select - Fatal编程技术网

Sql 带子查询的Oracle select

Sql 带子查询的Oracle select,sql,oracle,select,Sql,Oracle,Select,我有两个oracle select查询,如 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 =

我有两个oracle select查询,如

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
结果如下所示:

其他查询如下:

 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 
结果

我正在尝试将这两个
SELECT
查询组合起来,以便查看以下结果:


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

这两个查询几乎相同。结果可以在一个公共元素上连接在一起,因此可以将它们作为一个查询写入:

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'
这是一种古老的非标准方式来编写查询;看看内部连接关键字是如何工作的;下面是我如何提出这个问题的:

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'

我不确定提供的解决方案是否正确。他们都使用1连接到关联表。你需要两个。因为关联看起来是一个通用映射表,所以将位置连接到请求的行与将请求连接到实验的行不同。也许我错了,但我会:

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' 

这毫无意义。除了明显的输入错误(因此这些不是产生您显示的结果的查询,因为它们在语法上是不正确的),使用
ass.entity\u id\u 1
来匹配
位置视图
视图和
实验视图
视图有什么意义?您发现了哪些语法错误?对我来说,看起来有点老套@mathguy:考虑到这不是同一排,这是有道理的。位置中有一行,申请中有一行,实验中有一行,关联中有两行。关联中的第一行将loc链接到req,第二行将rep链接到exp。我个人讨厌这些多用途泛型关联表,但人们一直在这样做:)我尝试了第一个语句,没有使用连接,但它没有返回任何结果records@trx试试劳德克的答案;他注意到您的一个查询使用关联表,另一个使用关联视图表。我没有注意到这一点,这可能是一个关键的区别。。另一个很好的理由是您应该很好地布局sql;让他们更容易阅读!捕捉得好,你发现的差异非常细微。善行
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'