日期:2014-05-16  浏览次数:20723 次

请问个mysql两表关联问题
我有两表 company 与order,order中有company的外键,company_id,现在做两表联查,要求查出某时间段内的全部company的订单,如果订单数量为0则显示零。语句如下:

select c.id,c.name,c.code,sum(c.id) 
from company c left join order d on c.id=d.company_id
where d.gmt_created>"2012-06-01" 
and d.gmt_created<"2012-07-01" 
and (d.status_bcsticket in(2,4) or d.status_issueticket =5)(这个是查询条件)
group by c.id

想要的结果是这样的:

id name code sum  
001 a公司 00a 20
002 b公司 00b 0
003 c公司 00c 0
004 e公司 00e 10

但现在只能显示出这样:
id name code sum  
001 a公司 00a 20


大家有办法吗,谢谢指教,急用。

------解决方案--------------------
SQL code
select c.id,c.name,c.code,sum(c.id)  
from company c left join order d on c.id=d.company_id
and d.gmt_created>"2012-06-01"  
and d.gmt_created<"2012-07-01"  
and (d.status_bcsticket in(2,4) or d.status_issueticket =5)(这个是查询条件)
group by c.id