最初我們用天最多的如果沒用left join我們就會如
代碼如下 復制代碼select c.nom, e.nom
from consultant c, affaire a, besoin b, salarie sa, site s, entreprise e
where c.consultant_id=a.consultant_id and a.besoin_id=b.besoin_id and
b.salarie_id=sa.salarie_id and ssa.site_id=s.site_id and s.entreprise_id=e.entreprise_id
上面的寫法不好同時也不如left join效綠好
left join
代碼如下 復制代碼SELECT
A.ID, A.NUMBER, A.PRICE, A.ORDER_TIME, B.USER_ID, B.STARTIME, B.STOPTIME,
C.CHANNEL_PAY, D.COMPANY
FROM
D表 D
LEFT JOIN
A表 A ON A.COMPANY_ID = D.UID
LEFT JOIN
B表 B ON B.COMPANY_ID = D.UID
LEFT JOIN
C表 C ON C.COMPANY_ID = D.UID
有條件的再加行:
WHERE ……….
排序(如A表的ID由大到小排):
ORDER BY A.ID DESC
多表聯查的還可用inner join方法
代碼如下 復制代碼select c.nom, e.nom from consultant c inner join affaire a on c.consultant_id=a.consultant_id inner
join besoin b on a.besoin_id=b.besoin_id inner join salarie sa on b.salarie_id=sa.salarie_id inner
join site s on ssa.site_id=s.site_id inner join entreprise e on s.entreprise_id=e.entreprise_id
注意
多表聯合查詢有好多總方式right join、inner join、full join這裡不一一介紹了。