mysql - 如何计算一列中不同数据的总值(value)

标签 mysql sql

我有点困惑如何命名我的问题标题。

假设我的 MySQL 数据库表是这样的。 表组

UserID  UserGroup  UserStatus
------------------------------
1       1          1  
2       1          1 
3       1          2 
4       2          3
5       3          2
6       3          1
7       4          3
9       4          4
10      4          1 

我想按UserGroup对其进行分组,并计数UserStatus

让我知道获得这样的结果的正确说法是什么

UserGroup  Status_1  Status_2  Status_3  Status_4
-------------------------------------------------
1          2          2        0         0
2          0          0        1         0
3          1          1        0         0
4          1          0        1         1

最佳答案

SELECT UserGroup, count(UserStatus = 1) as Status_1, count(UserStatus = 2) as Status_2,
                  count(UserStatus = 3) as Status_3, count(UserStatus = 4) as Status_4
FROM table_group GROUP BY UserGroup

关于mysql - 如何计算一列中不同数据的总值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6724955/

相关文章:

mysql - 进行 mySQL 查询来获取尚未售出的产品?

mysql - 什么数据库用于点击流应用程序 RELATIONAL 或 NOSQL?

php - 管理页面上的用户表,需要很长时间才能加载

sql - 如何格式化 SQL 插入以对齐逗号分隔符?

php - 当 SQL 查询为空时使用 PHP 重定向

SQL查询以获取当前和去年的销售额

MySQL REPLACE 语句不正确? "A new statement was found, but no delimiter"

php - CakePHP 中的 MySQL 连接 -> 为什么我不断收到超过 30 秒的 fatal error ?

sql - 如何向表中插入混合查询

sql - Arel:使用符号的左外连接