最近在做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 |
5 | order by b.sort_order asc |
其中,表shop_goods是存储了所有商品信息的数据表,shop_recommended_goods则是将商品id与推荐id相关联的表。
MySQL多表联查例子:
下面这两个MySQL多表联查方法都可以,inner join on 更好点。表结构没贴出来,但比较好懂了。
MySQL多表联查的简单方法:
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方法:
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 |