SQL查询聚合函数

时间:2015-07-09 08:19:58

标签: mysql

这是我的疑问:

select a.id, a.title,count(x.id) as Orders
-- from x_vendordeliveriesareas vda
-- left join x_vendordeliveries vd
-- on vd.id = vda.vendordelivery_id
from x_orders x
left join x_areas a
on x.delivery_area_id  = a.id
-- on vda.area_id = a.id
-- left join x_orders x
left join x_vendors v
on v.id = x.vendor_id
where v.title like 'golden dragon%' and (date_format(x.date,'%Y-%m-%d') BETWEEN '2015-01-01' AND '2015-06-30') 
and x.status_id=11 
and x.country_id =11
and v.city_id=3
group by 1;

这很好用,但我想返回那些有0个订单的区域。 我尝试过IFNULL并合并函数

1 个答案:

答案 0 :(得分:2)

您的驾驶表应为$(document).ready(function() {

x_areas

请注意,我移动了select a.id, a.title, coalesce(count(x.id), 0) as Orders from x_areas a left join x_orders x on x.delivery_area_id = a.id and x.status_id = 11 and x.country_id = 11 and (date_format(x.date,'%Y-%m-%d') BETWEEN '2015-01-01' AND '2015-06-30') left join x_vendors v on v.id = x.vendor_id where v.title like 'golden dragon%' and v.city_id = 3 group by 1; 条款中的部分WHERE条件,以防止ON变成LEFT JOIN,从而产生相同的结果已添加INNER JOIN 0订单。