sql查询中case when...else...end

来源:互联网 发布:接入网络方式 编辑:程序博客网 时间:2024/05/22 13:29

用于并列查询
有如下person表,查询出类型为1和2的各占多少
id type name
1 1 li
2 1 jackson
3 1 mark
4 1 didong
5 1 hanmeimei
6 2 lilei
7 2 polly
8 2 park

select count(case when t.type='1' then t.name else '' end) count1,count(case when t.type='2' then t.name else '' end) count2from person t

result:
count1 5
count2: 3

0 0
原创粉丝点击