Home > Database > Mysql Tutorial > body text

mysql中left join多表联查询语句

WBOY
Release: 2016-06-07 17:52:44
Original
2357 people have browsed it

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
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这里不一一介绍了。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template