输入banner图图片脚本导航/分类

统计某一字段等于不同值的个数的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