Heim >Datenbank >MySQL-Tutorial >统计某一字段等于不同值的个数的sql语句(分享)_MySQL

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-01 13:29:581451Durchsuche

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
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn