原表
居委 注册 可用
df 可注册 已外用
df 已注册 已自用
fg 可注册 闲置
fg 已注册 闲置
这是统计出来的表
居委 可注册 已注册 已外用 已自用 闲置
内容 数量 数量 数量 数量 数量
df 1 1 1 1 1
fg 1 1 1 1 2
居委 注册 可用
df 可注册 已外用
df 已注册 已自用
fg 可注册 闲置
fg 已注册 闲置
这是统计出来的表
居委 可注册 已注册 已外用 已自用 闲置
内容 数量 数量 数量 数量 数量
df 1 1 1 1 1
fg 1 1 1 1 2
+------+----------+--------+
| name | registry | isable |
+------+----------+--------+
| df | 可注册 | 已外用 |
| df | 已注册 | 已自用 |
| fg | 可注册 | 闲置 |
| fg | 已注册 | 闲置 |
+------+----------+--------+
4 rows in set (0.00 sec)mysql> select name,sum(case when registry='可注册' then 1 else 0 end) as ableReg,
-> sum(case when registry='已注册' then 1 else 0 end) as alreadyReg,
-> sum(case when isable='已外用' then 1 else 0 end) as outUse,
-> sum(case when isable='已自用' then 1 else 0 end) as inUse,
-> sum(case when isable='闲置' then 1 else 0 end) as free
-> from st
-> group by name;
+------+---------+------------+--------+-------+------+
| name | ableReg | alreadyReg | outUse | inUse | free |
+------+---------+------------+--------+-------+------+
| df | 1 | 1 | 1 | 1 | 0 |
| fg | 1 | 1 | 0 | 0 | 2 |
+------+---------+------------+--------+-------+------+
2 rows in set (0.00 sec)