Home  >  Article  >  Database  >  MySQL的双表多表联查

MySQL的双表多表联查

WBOY
WBOYOriginal
2016-06-07 16:24:222441browse

最近在做EC-Mall的二次开发,遇到这么一个需求,将挂件单独显示成一个页面。由于EC-Mall的挂件是用数据模块+模块类库的方式进行的,就是使用类似smarty的形式。而单独一个页面的话,数据读取需要自己写SQL语句。 现在的问题是,需要将商品中的汽车类中的推荐

最近在做EC-Mall的二次开发,遇到这么一个需求,将挂件单独显示成一个页面。由于EC-Mall的挂件是用数据模块+模块类库的方式进行的,就是使用类似smarty的形式。而单独一个页面的话,数据读取需要自己写SQL语句。

现在的问题是,需要将商品中的汽车类中的推荐商品数据显示出来,sql如下:

select * from shop_goods as a 
	join shop_recommended_goods as b 
    where a.goods_id = b.goods_id 
    and b.recom_id = 36 
    order by b.sort_order asc 
    limit 14

其中,表shop_goods是存储了所有商品信息的数据表,shop_recommended_goods则是将商品id与推荐id相关联的表。

MySQL多表联查例子:

下面这两个MySQL多表联查方法都可以,inner join on 更好点。表结构没贴出来,但比较好懂了。

MySQL多表联查的简单方法:

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  

MySQL多表联查的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  
Statement:
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