[英]Oracle select with sub queries
我有兩個甲骨文選擇查詢
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
查詢,以便我看到這些結果:
我應該使用子查詢來查看組合結果還是有其他優化方法?
我不確定所提供的解決方案是否正確。 他們都使用1連接到關聯表。 您需要2。因為關聯看上去是一個通用的映射表,所以將位置鏈接到申請的行與將請求鏈接到實驗的行不同。 也許我錯了,但我會去:
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'
這兩個查詢幾乎相同。 結果可以在一個公共元素上連接在一起,因此,可以將它們編寫為一個查詢:
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'
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.