繁体   English   中英

MySQL。 我如何 COUNT() 但在不同的列上插入不同的值?

[英]MySQL. How can I COUNT() but insert different value on the different column?

表用户

|   id   |   username   |   status   |   
|   1    |   cahya      |   steady   |
|   2    |   kadek      |   steady   |
|   3    |   cahya      |   steady   |
|   4    |   erno       |   ready    |
|   5    |   kadek      |   go       |

跳跃结果如下

| username | steady | ready | go |
|  cahya   |   2    |   0   | 0  |
|  kadek   |   1    |   0   | 1  |
|  erno    |   0    |   1   | 0  |

我是分开做的,但我认为这不是一部好作品

select username, count(status) from users where status = 'steady'
select username, count(status) from users where status = 'ready'
select username, count(status) from users where status = 'go'
SELECT username, 
       SUM(status = 'steady') AS steady, 
       SUM(status = 'ready') AS ready, 
       SUM(status = 'go') AS go
FROM users 
GROUP BY 1;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM