您好,欢迎来到曲格情感。
搜索
您的当前位置:首页统计某一字段等于不同值的个数的sql语句(分享)_MySQL

统计某一字段等于不同值的个数的sql语句(分享)_MySQL

来源:曲格情感


bitsCN.com
本文介绍下,用一条sql语句统计某一字段等于不同值的个数,方法很独特,有需要的朋友参考下。表t,数据: id type
001 1
001 0
002 1
001 0
002 0
001 1
001 0
002 0

要求:
统计不同id,type分别为0的,1的个数。

sql语句:

代码示例:select
id,sum(case when type=0 then 1 else 0 end) as 0,sum(case when type=1 then 1 else 0 end) as 1
from t
group by id

查询结果:

id 0 1
001 3 2
002 2 1

本文出处参考:http://www.jbxue.com/db/11867.html

bitsCN.com

Copyright © 2019- qugedi.com 版权所有

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务