dongyan2445 2014-08-08 13:23
浏览 71
已采纳

列中的COUNT值和生成结果表mysql

To preface this question - I know I'm missing something really obvious here but it is Friday afternoon!

I'm trying to COUNT to number of times certain values appear within the same column and produce a table of the results.

Here's my code so far:

MYSQL

SELECT COUNT(order_status) as printed
FROM orders WHERE order_status = 'Printed Order'

UNION

SELECT COUNT(order_status) as charged
FROM orders WHERE order_status = 'Charged Order'

UNION

SELECT COUNT(order_status) as exchanged
FROM orders WHERE order_status = 'Exchanged Order'

UNION

SELECT COUNT(order_status) as refunded
FROM orders WHERE order_status = 'Refunded Order'

UNION

SELECT COUNT(order_status) as cancelled
FROM orders WHERE order_status = 'Cancelled Order'
GROUP BY order_status

Result of the above query

printed
-------
224
19190
593
2618
2899

The code is producing the correct figures, however, I would prefer the result to look as follows:

Desired result

printed - 224
charged - 19190
exchanged - 593
refunded - 2618
cancelled -2899

This way I can easily reference them via associative array call i.e. $order_status['printed']

Any help would be great.

  • 写回答

3条回答 默认 最新

  • douqilin4296 2014-08-08 13:27
    关注

    Add a column specifying the type. The easiest way is to use group by:

    select order_status, count(*)
    from orders o
    group by order_status;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改