id type resource
1 1 1
1 1 1
2 2 2
select id,count(distinct resource) as c
from tab_name
where type=1
group by id;
id c
1 2
我想让其显示为
id c
1 2
2 0
希望我的问题描述清楚
[b]问题补充:[/b]
回复一楼:
条件不能去掉
没看清需求不好意思.....
[code="sql"]
select a.id,
count(distinct case type when 1 then resource else NULL END) as c
from tab_name a
group by id
[/code]
把type=1去掉不就成了。。。。
或者写成
[code="sql"]
select a.id, count(distinct b.resource)
from tab_name a, tab_name b
where a.id *= b.id
and b.type = 1 group by a.id
[/code]
再或者写成自查询 .... 随你便啦...