如何水平显示MYSQL COUNT

时间:2012-02-26 10:15:57

标签: mysql count

您好我试图以横向方式显示COUNT个结果,但没有成功。有没有简单的方法呢?

我有这个表(在excel中创建用于演示目的)

enter image description here

现在我想显示这样的结果。

enter image description here

任何建议都将不胜感激。

2 个答案:

答案 0 :(得分:5)

SELECT store_name,
       SUM(CASE WHEN status = 'hold' THEN 1 ELSE 0 END) AS hold_count,
       SUM(CASE WHEN status = 'ship' THEN 1 ELSE 0 END) AS ship_count,
       SUM(CASE WHEN status = 'return' THEN 1 ELSE 0 END) AS return_count
FROM table
group by store_name

答案 1 :(得分:2)

在sum-aggregate中使用if子句。

select store_name,
  sum(if(status='hold', 1, 0)) as 'hold',
  sum(if(status='ship', 1, 0)) as 'ship',
  sum(if(status='return', 1, 0)) as 'return'
from mytable
group by store_name;