sql三表联合查询

一个主表,一个附表,一个中间表

表结构 主表中有mobile,中间表有mobile,id,附表中有id(is_defalut=1,保证只有一条)

条件保证主表一定显示,附表、中间表都可以为空

select a.*,d.* from a left join (select c.*,b.mobile from b,c where b.id=c.id and c.is_default=1) d on a.mobile=d.mobile

实例:

select  a.*,d.consignee shname,d.mobile shmobile,d.province shprovince,d.city shcity,d.district shdistrict,d.address shaddress FROM tp_signup a LEFT JOIN (select c.consignee consignee,c.province province,c.city city,c.district district,c.address address,b.mobile mobile FROM  tp_users b,tp_user_address c where  b.user_id=c.user_id and c.is_default=1) d ON a.mobile=d.mobile

猜你喜欢

转载自my.oschina.net/u/210357/blog/828249