2013-01-25 41 views
2

我使用Oracle SQL Developer和我試圖像這樣運行的SQLDeveloper輸出的變化選擇特定的列

SELECT * FROM 
    (SELECT id1 CONTRACT_ID1, id2 CONTRACT_ID2 FROM contract_table) 
    LEFT JOIN 
    (SELECT id1 A_CONTRACT_ID, id2 A_SPECIAL_CONTRACT_ID from special_contract_table_a) 
    on (CONTRACT_ID1 = A_CONTRACT_ID) 
    LEFT JOIN 
    (SELECT id1 B1_CONTRACT_ID, id2 B1_DOCUMENT_ID from document_table_b1) 
    ON (CONTRACT_ID2 = B1_CONTRACT_ID) 
    LEFT JOIN 
    (SELECT id1 B2_CONTRACT_ID, id2 B2_DOCUMENT_ID from document_table_b2) 
    ON (A_SPECIAL_CONTRACT_ID = B2_CONTRACT_ID) 
    LEFT JOIN 
    (SELECT id1 B3_CONTRACT_ID, id2 B3_DOCUMENT_ID from document_table_b3) 
    ON (A_SPECIAL_CONTRACT_ID = B3_CONTRACT_ID) 
    LEFT JOIN 
    (SELECT page PAGE, id1 C1_DOCUMENT_ID, id2 C2_DOCUMENT_ID, id3 C3_DOCUMENT_ID from page_table_c) 
    ON (C1_DOCUMENT_ID = B1_DOCUMENT_ID OR C2_DOCUMENT_ID = B2_DOCUMENT_ID OR C3_DOCUMENT_ID = B3_DOCUMENT_ID) 
    LEFT JOIN 
    (SELECT id2 B1_DOCUMENT_ID_2 from, status B1_DOCUMENT_STATUS from document_table_b1) 
    ON (C1_DOCUMENT_ID = B1_DOCUMENT_ID_2) 
    LEFT JOIN 
    (SELECT id2 B2_DOCUMENT_ID_2 from, status B2_DOCUMENT_STATUS from document_table_b2) 
    ON (C2_DOCUMENT_ID = B2_DOCUMENT_ID_2) 
    LEFT JOIN 
    (SELECT id2 B3_DOCUMENT_ID_2 from, status B3_DOCUMENT_STATUS from document_table_b3) 
    ON (C3_DOCUMENT_ID = B3_DOCUMENT_ID_2) 
; 

所以基本上這樣做是什麼,那我選擇我的兩個基地的ID查詢時,則獲得所有相關組的所有成員,然後獲取每個成員的狀態(並且狀態存儲在組聲明中)。如我所料,只要我使用SELECT * FROM即可。如果我將其更改爲SELECT CONTRACT_ID1, PAGE, B1_DOCUMENT_STATUS, B2_DOCUMENT_STATUS, B3_DOCUMENT_STATUS,則輸出從24行摺疊爲4,僅顯示document_table_b1的匹配項。

任何想法如何解決這個問題?

其他信息:DB-Version是Oracle 10g。 Oracle SQL Developer版本是3.2.09。 Autotrace和Explainplan之間的比較顯示了一些訪問謂詞從ID_1(+) = ID_2(Explainplan)到ID_1 = ID_2(Autotrace)與兩個select語句的差異。 我高度懷疑查詢優化器在這裏搞砸了。我還沒有找到修補其設置的方法。朝這個方向的任何輸入可能會有所幫助。

+0

能爲您的選擇子明確別名試試嗎? –

+0

@DanielHilgarth完成!現在好多了? –

+1

呃?你改變了什麼?我要求你檢查當你給你的子查詢一個明確的別名時是否仍然有錯誤...我沒有要求你編輯你的問題...你的代碼:(從foo選擇x)left outer join( (x = y)上選擇y。我在問什麼:(從foo選擇x)**作爲**左外連接(從bar中選擇y)**作爲b ** on(a.x = b.y) –

回答

2

哦,它使不可讀的,你爲什麼把每個表爲select語句

SELECT a.id1 as CONTRACT_ID1 
,ptc.page as PAGE 
,b1.status as B1_DOCUMENT_STATUS 
,b2.status as B2_DOCUMENT_STATUS 
,b3.status as B3_DOCUMENT_STATUS 
FROM 
    contract_table ct 
    LEFT JOIN special_contract_table_a scta on scta.id1 = ct.id1 
    LEFT JOIN document_table_b1 b1 ON b1.id1 = ct.id2 
    LEFT JOIN document_table_b2 b2 ON b2.id1 = scta.id2 
    LEFT JOIN document_table_b3 b3 ON b3.id1 = scta.id2 
    LEFT JOIN page_table_c ptc ON c.id1 = b1.id1 or c.id2= b2.id1 or c.id3 = b3.id1 
    where (YoursPredicateHere) 
    --dont understand why you are joining them again 
    --LEFT JOIN document_table_b1 b1_2 ON (C1_DOCUMENT_ID = B1_DOCUMENT_ID_2) 
    --LEFT JOIN document_table_b2 b2_2 ON (C2_DOCUMENT_ID = B2_DOCUMENT_ID_2) 
    --LEFT JOIN document_table_b3 b3_2 ON (C3_DOCUMENT_ID = B3_DOCUMENT_ID_2) 
+0

你可以在你的語法中加入where子句嗎? –

+0

,你想把什麼放在where子句中? – WKordos

+0

看到你的編輯,好點!謝謝! –