mysql多表 子查询 左连接

问题:要做一个mysql的复杂查询 多表查询 且包含 子连接

解决:

select c.*,d1.debt,d2.hasorders from dw_customerinfo c left join (select d.customerid, sum(d.price)>sum(d.pay) debt from dw_ordersdetail d where d.state='1' and d.pubtime<= now() group by d.customerid) d1 on c.id=d1.customerid 
left join (select d.customerid, count(d.customerid)>0 hasorders from dw_ordersdetail d group by d.customerid) d2 on  c.id=d2.customerid

 方案1:ps:  测试未通过

Select * FROM t1 LEFT JOIN (t2, t3, t4) ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c)

 

方案2:ps:测试通过

SELECT artists.Artist, cds.title, genres.genre 
FROM cds 
LEFT JOIN genres N cds.genreID = genres.genreID 
LEFT JOIN artists ON cds.artistID = artists.artistID; 

 

参考资料:

Mysql 多表联合查询效率分析

http://blog.csdn.net/hguisu/archive/2010/07/13/5731880.aspx

MySQL多表连接查询Left Join,Right Join

http://blog.sina.com.cn/s/blog_4933585b0100dfon.html

你可能感兴趣的:(C++,c,mysql,C#,Blog)