2011-12-08 236 views
1

我有五個表,我想從中得到結果。下面是我在做什麼:

Oracle SQL查詢(左外部連接)

 
select 
     person.SERVICE_NO as Service_No, person.CNIC_NO as CNIC, person.NAME as NAME , card.CPLC_SERIAL_NO as Card_Number, 
     child_dc.NAME as Child_DC, root_dc.NAME as Root_DC, person.OU as OU, person.EMAIL as Email 
from 
     person,card,person_card,child_dc,root_dc 
where 
     person_card.PERSON_ID = person.ID 
and 
     person_card.CARD_ID = card.ID 
and 
     person.CHILD_DC_ID = child_dc.ID 
and 
     root_dc.ID = child_dc.ID; 

該查詢給冗餘值,(如果沒有我的地方明顯與它)。我正在考慮這樣做,放棄加入;這意味着我將與5張桌子左外接合。我將如何做到這一點。如果任何人有更優化的查詢或任何其他想法,那就太好了。

回答

2

查詢更新時間:

select distinct 
    person.SERVICE_NO as Service_No, 
    person.CNIC_NO as CNIC, person.NAME as NAME , 
    card.CPLC_SERIAL_NO as Card_Number, 
    child_dc.NAME as Child_DC, 
    root_dc.NAME as Root_DC, person.OU as OU, 
    person.EMAIL as Email 
from 

    person_card inner join person 
    on person_card.PERSON_ID = person.ID 
    inner join card 
    on person_card.CARD_ID = card.ID 
     left outer join child_dc 
    on person.CHILD_DC_ID = child_dc.ID 
     left outer join root_dc 
    on child_dc.ID = root_dc.ID; 
+0

不工作,它缺少一個關鍵的字。 –

+0

立即嘗試。我錯過了關鍵字 – reggie

+0

只是注意到,你忘了放在最後一行,而且,如果我沒有放置不同,它的一團糟。您如何按照我的查詢優化您的查詢,因爲兩者的結果都是一樣的。 –