mysql统计同一字段下,不同值的总数的方法:
basebandBoard为字段,BPN2和CCE1等是为字段的多个值,这里进行统计。
方法一:
use gc; select basebandBoard,count(basebandBoard) co from four where basebandBoard='CCE1'; select basebandBoard,count(basebandBoard) co from four where basebandBoard='BPN2'; select basebandBoard,count(case when basebandBoard='BPN2' then 1 end) as BPN2, count(case when basebandBoard='CCE1' then 1 end) as CCE1, count(case when basebandBoard='BPP1' then 1 end) as BPP1 from four group by basebandBoard;
效果:
方法二:
select count(if(basebandBoard='BPN2',basebandBoard,null)),count(if(basebandBoard='CCE1',basebandBoard,null)),count(if(basebandBoard='BPP1',basebandBoard,null)) from four;
效果:【这样的显示可以有助于知道if的用法,后续实际使用可以取别名】
命别名后:
select count(if(basebandBoard='BPN2',basebandBoard,null)) BPN2,count(if(basebandBoard='CCE1',basebandBoard,null)) BPN2,count(if(basebandBoard='BPP1',basebandBoard,null)) BPP1 from four;
效果: