您的位置:首页 > 数据库 > MySQL

MySQL的双表多表联查

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

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

1
select
 
from
 
shop_goods 
as
 
a
2
    
join
 
shop_recommended_goods 
as
 
b
3
    
where
 
a.goods_id
= b.goods_id
4
    
and
 
b.recom_id
= 36
5
    
order
 
by
 
b.sort_order 
asc
6
    
limit
14
其中,表shop_goods是存储了所有商品信息的数据表,shop_recommended_goods则是将商品id与推荐id相关联的表。

MySQL多表联查例子:

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

MySQL多表联查的简单方法:

1
select
 
c.nom,
e.nom  
2
from
 
consultant
c, affaire a, besoin b, salarie sa, site s, entreprise e 
3
where
 
c.consultant_id=a.consultant_id 
and
 
a.besoin_id=b.besoin_id 
and
  
4
b.salarie_id=sa.salarie_id 
and
 
ssa.site_id=s.site_id 
and
s.entreprise_id=e.entreprise_id 
MySQL多表联查的inner join方法:

view
source

print?

1
select
 
c.nom,
e.nom 
2
from
 
consultant
c 
3
inner
 
join
 
affaire
a 
on
 
c.consultant_id=a.consultant_id 
4
inner
 
join
 
besoin
b 
on
 
a.besoin_id=b.besoin_id 
5
inner
 
join
 
salarie
sa 
on
 
b.salarie_id=sa.salarie_id 
6
inner
 
join
 
site
s 
on
 
ssa.site_id=s.site_id 
7
inner
 
join
 
entreprise
e 
on
 
s.entreprise_id=e.entreprise_id 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: