gridview yii2中每个状态的计数

时间:2017-01-12 10:24:23

标签: yii2

在第一列的gridview中是一个国家/地区列表。下一列是状态列。在sql我得到它像这样

SELECT COUNT(*) FROM order_customFields
INNER JOIN `order` ON order_customFields.order_id = `order`.order_id
WHERE
order_customFields.order_customFields_delivery_method = 'us' AND
order_customFields.order_customFields_order_date >= '2016-12-01' AND
order_customFields.order_customFields_order_date <= '2016-12-31' AND
`order`.order_status = 'sent'

我如何获得每个州和每个国家的订单数量?像这样enter image description here

1 个答案:

答案 0 :(得分:1)

SELECT 
    order_customFields.order_customFields_delivery_method,
    sum(case `order`.order_status when 'new' then 1 else 0 end) newtcount,
    sum(case `order`.order_status when 'arrived' then 1 else 0 end) arrivedcount,
    sum(case `order`.order_status when 'sent' then 1 else 0 end) sentcount
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' 
GROUP BY    
   order_customFields.order_customFields_delivery_method,
   `order`.order_status