【问题标题】:Group By results as individual column namesGroup By 结果作为单独的列名
【发布时间】:2013-07-01 10:23:06
【问题描述】:

如何获取 groupby 结果作为列名

如果我们使用的时候有职员表

按性别从员工组中选择计数(*);给我作为

但我需要它们作为列女 |无

【问题讨论】:

  • 考虑在应用程序级别处理数据显示问题(例如,使用一点 PHP)

标签: mysql


【解决方案1】:
select sum(case when gender = 'Male' then 1 else 0 end) as 'Male',
sum(case when gender = 'Female' then 1 else 0 end) as 'Female',
sum(case when gender not in ('Male','Female') or gender is null then 1 else 0 end) as 'None'
from staff;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    相关资源
    最近更新 更多