bitscn.com
一条sql语句实现统计查询
如图:程序员在进行如下的统计时,现在提供两种实现方案:
方案一:运用 sekect case when
explain select count(*) as '总数', count( case oup.status when '1' then oup.id end ) as '未绑定', count( case oup.status when '2' then oup.id end ) as '已绑定', count( case oup.status when '3' then oup.id end )as '冻结中' from lab_org_uc_passport oup
显示结果:(按行显示)
方案二:
select count(*) as '总数' from lab_org_uc_passport oup union all select count(*) as '未绑定' from lab_org_uc_passport oup where oup.status = '1' union all select count(*) as '未绑定' from lab_org_uc_passport oup where oup.status = '2' union all select count(*) as '未绑定' from lab_org_uc_passport oup where oup.status = '3'
显示结果(按列显示)
34
3
10
21
bitscn.com