我有五张桌子,我希望得到结果。这就是我在做什么:
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张桌子进行LEFT OUTER JOINING。我该怎么做如果有人有更优化的查询或任何其他想法,那就太棒了。
答案 0 :(得分: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;