2014-11-20 201 views
0

我有表避免在查詢

M,R,G,d,PT和PI

PI具有與所有其他表的關係。 有一個父 - M,R,G和D的子關係

實施例:型號ID將在那裏在M和R 範圍ID將在那裏在R和G 級ID將在那裏在G中和d

要知道其被髮布的衍生物,示例是如下

SELECT D.DERIVATIVE_ID 
FROM PRD.DERIVATIVE D 
     JOIN PUB.PUBLISHED_ITEM PI ON PI.PUBLISHED_OBJECT_ID = D.DERIVATIVE_ID 
             AND PI.PUBLISHED_OBJECT_TYPE = 'DERIVATIVE' 
     JOIN PUB.PUBLISHED_TARGET PT ON PI.PUBLISHED_TARGET_ID = PT.PUBLISHED_TARGET_ID 
WHERE D.DERIVATIVE_ID = 683239 

要知道其被髮布的模型,一個例子是如下面

SELECT * 
FROM PRD.MODEL M 
     JOIN PUB.PUBLISHED_ITEM PI ON PI.PUBLISHED_OBJECT_ID = M.MODEL_ID 
             AND PI.PUBLISHED_OBJECT_TYPE = 'MODEL' 
     JOIN PUB.PUBLISHED_TARGET PT ON PI.PUBLISHED_TARGET_ID = PT.PUBLISHED_TARGET_ID 
WHERE M.model_id = 257 

我想編寫一個查詢,顯示模式,其衍生物公佈,但不是模型(模型沒有公佈)

這裏是我試過了,您的意見將是有益的

SELECT * 
FROM PRD.MODEL M 
     JOIN PUB.PUBLISHED_ITEM PI ON PI.PUBLISHED_OBJECT_ID = M.MODEL_ID 
             AND PI.PUBLISHED_OBJECT_TYPE = 'MODEL' 
     JOIN PUB.PUBLISHED_TARGET PT ON PI.PUBLISHED_TARGET_ID <> PT.PUBLISHED_TARGET_ID 
     JOIN PRD.RANGE R ON R.MODEL_ID = M.MODEL_ID 
     JOIN PRD.GRADE G ON G.RANGE_ID = R.RANGE_ID 
     JOIN prd.DERIVATIVE D ON D.GRADE_ID = G.GRADE_ID 
           AND D.DERIVATIVE_ID IN (
           SELECT D.DERIVATIVE_ID 
           FROM PRD.DERIVATIVE D 
             JOIN PUB.PUBLISHED_ITEM PI ON PI.PUBLISHED_OBJECT_ID = D.DERIVATIVE_ID 
                   AND PI.PUBLISHED_OBJECT_TYPE = 'DERIVATIVE' 
             JOIN PUB.PUBLISHED_TARGET PT ON PI.PUBLISHED_TARGET_ID = PT.PUBLISHED_TARGET_ID) 

同樣在上面的查詢中,我該如何避免使用「IN」?

+0

THX重新格式化,你用什麼工具? – Beth 2014-11-20 17:25:33

+0

我使用SQL提示 - 非常值得花錢! – 2014-11-20 17:30:09

回答

0

通常的方法是EXISTS

SELECT * 
FROM PRD.MODEL M 
JOIN PUB.PUBLISHED_ITEM PI 
    ON PI.PUBLISHED_OBJECT_ID = M.MODEL_ID 
     AND PI.PUBLISHED_OBJECT_TYPE = 'MODEL' 
JOIN PUB.PUBLISHED_TARGET PT 
    ON PI.PUBLISHED_TARGET_ID <> PT.PUBLISHED_TARGET_ID 
JOIN PRD.RANGE R 
    ON R.MODEL_ID = M.MODEL_ID 
JOIN PRD.GRADE G 
    ON G.RANGE_ID = R.RANGE_ID 
JOIN prd.DERIVATIVE D 
    ON D.GRADE_ID = G.GRADE_ID 
WHERE EXISTS (
     SELECT 1 
     FROM PRD.DERIVATIVE D_s 
     JOIN PUB.PUBLISHED_ITEM PI_s 
      ON PI_s.PUBLISHED_OBJECT_ID = D_S.DERIVATIVE_ID 
      AND PI_s.PUBLISHED_OBJECT_TYPE = 'DERIVATIVE' 
     JOIN PUB.PUBLISHED_TARGET PT_s 
      ON PI_s.PUBLISHED_TARGET_ID = PT_s.PUBLISHED_TARGET_ID 
     WHERE D_s.DERIVATIVE_ID = D.DERIVATIVE_ID 
     ) 
0

使用INNER Join可以避免。

SELECT * 
FROM PRD.MODEL M 
     JOIN PUB.PUBLISHED_ITEM PI 
     ON PI.PUBLISHED_OBJECT_ID = M.MODEL_ID 
      AND PI.PUBLISHED_OBJECT_TYPE = 'MODEL' 
     JOIN PUB.PUBLISHED_TARGET PT 
     ON PI.PUBLISHED_TARGET_ID <> PT.PUBLISHED_TARGET_ID 
     JOIN PRD.RANGE R 
     ON R.MODEL_ID = M.MODEL_ID 
     JOIN PRD.GRADE G 
     ON G.RANGE_ID = R.RANGE_ID 
     JOIN prd.DERIVATIVE d 
     ON d.GRADE_ID = g.GRADE_ID 
     JOIN (SELECT DISTINCT D.DERIVATIVE_ID 
      FROM PRD.DERIVATIVE D 
        JOIN PUB.PUBLISHED_ITEM PI 
         ON PI.PUBLISHED_OBJECT_ID = D.DERIVATIVE_ID 
         AND PI.PUBLISHED_OBJECT_TYPE = 'DERIVATIVE' 
        JOIN PUB.PUBLISHED_TARGET PT 
         ON PI.PUBLISHED_TARGET_ID = PT.PUBLISHED_TARGET_ID) AA 
     ON D.DERIVATIVE_ID = AA.DERIVATIVE_ID 

通過使用Exists

SELECT * 
FROM PRD.MODEL M 
     JOIN PUB.PUBLISHED_ITEM PI 
     ON PI.PUBLISHED_OBJECT_ID = M.MODEL_ID 
      AND PI.PUBLISHED_OBJECT_TYPE = 'MODEL' 
     JOIN PUB.PUBLISHED_TARGET PT 
     ON PI.PUBLISHED_TARGET_ID <> PT.PUBLISHED_TARGET_ID 
     JOIN PRD.RANGE R 
     ON R.MODEL_ID = M.MODEL_ID 
     JOIN PRD.GRADE G 
     ON G.RANGE_ID = R.RANGE_ID 
     JOIN prd.DERIVATIVE D 
     ON D.GRADE_ID = G.GRADE_ID 
WHERE EXISTS (SELECT 'X' 
       FROM PRD.DERIVATIVE ED 
         JOIN PUB.PUBLISHED_ITEM EPI 
         ON EPI.PUBLISHED_OBJECT_ID = ED.DERIVATIVE_ID 
          AND EPI.PUBLISHED_OBJECT_TYPE = 'DERIVATIVE' 
         JOIN PUB.PUBLISHED_TARGET EPT 
         ON EPI.PUBLISHED_TARGET_ID = EPT.PUBLISHED_TARGET_ID 
       WHERE ED.DERIVATIVE_ID = D.DERIVATIVE_ID) 
+0

謝謝你的迴應,他們很有幫助 – Techie 2014-11-21 10:50:35