查询中的总和

时间:2017-01-20 09:56:29

标签: mysql

我有这个

    SELECT order_customFields.order_customFields_delivery_method,

    sum(case `order`.order_status when 'paid' then 1 else 0 end) paid,
    sum(case `order`.order_status when 'later' then 1 else 0 end) later,
  sum(case `order`.order_status when 'delivery-approved' then 1 else 0 end) deliveryapproved,
  sum(case `order`.order_status when 'problem' then 1 else 0 end) problem
FROM order_customFields 
  INNER JOIN `order` ON order_customFields.order_id = `order`.order_id
WHERE
  order_customFields.order_customFields_order_date >= '2016-12-01' AND
  order_customFields.order_customFields_order_date <= '2016-12-31' 
AND order_customFields.order_customFields_delivery_method is not null
GROUP BY    
   order_customFields.order_customFields_delivery_method

看起来像enter image description here

如何在查询中获取另一个字符串,例如&#39; Europe&#39;,其中单元格是快递员的汇总(所有&#39; eu&#39;)?例如欧洲 - 支付1471

1 个答案:

答案 0 :(得分:1)

你可以使用union和一个合适的finter

SELECT order_customFields.order_customFields_delivery_method,
      sum(case `order`.order_status when 'paid' then 1 else 0 end) paid,
      sum(case `order`.order_status when 'later' then 1 else 0 end) later,
      sum(case `order`.order_status when 'delivery-approved' then 1 else 0 end) deliveryapproved,
      sum(case `order`.order_status when 'problem' then 1 else 0 end) problem
FROM order_customFields 
  INNER JOIN `order` ON order_customFields.order_id = `order`.order_id
WHERE
  order_customFields.order_customFields_order_date >= '2016-12-01' AND
  order_customFields.order_customFields_order_date <= '2016-12-31' 
AND order_customFields.order_customFields_delivery_method is not null
GROUP BY    
   order_customFields.order_customFields_delivery_method

UNION 


SELECT 'EUROPE',
      sum(case `order`.order_status when 'paid' then 1 else 0 end) paid,
      sum(case `order`.order_status when 'later' then 1 else 0 end) later,
      sum(case `order`.order_status when 'delivery-approved' then 1 else 0 end) deliveryapproved,
      sum(case `order`.order_status when 'problem' then 1 else 0 end) problem
FROM order_customFields 
  INNER JOIN `order` ON order_customFields.order_id = `order`.order_id
WHERE
  order_customFields.order_customFields_order_date >= '2016-12-01' AND
  order_customFields.order_customFields_order_date <= '2016-12-31' 
AND order_customFields.order_customFields_delivery_method is not null
AND substr(order_customFields.order_customFields_delivery_method,1,3) ='eu_'
GROUP BY    
   substr(order_customFields.order_customFields_delivery_method,1,3)