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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-01 13:29:58
Original
1447 people have browsed it

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
Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template