left join是mysql中一个常用的数据库联合多表查询的命令,下面我来详细的介绍left join用法有需要的朋友可参考.
最初我们最多的如果没用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 --Vevb.com
- 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这里不一一介绍了.
新闻热点
疑难解答