SQL 查询使用COUNT函数一个方法

select 字段a,count(表1.字段b) as c from 表1 inner join 表2 on 表1.字段=表2.字段

比如一个查询语句是这个格式,想要在count()查询出的数据上再加一个条件,where应该怎么写?或者要怎么改一下?
where条件只针对count()出来的c那一列,不能影响到字段a那一列

select 字段a,
count(case when (条件) then 表1.字段b end) as c
from 表1 inner join 表2 on 表1.字段=表2.字段
可以用case加条件。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-01-18
简单
select * from(select 字段a,count(表1.字段b) as c from 表1 inner join 表2 on 表1.字段=表2.字段)
as x where x.c 加条件
第2个回答  2011-01-18
select 字段a,count(表1.字段b) as c from 表1 inner join 表2 on 表1.字段=表2.字段 WHERE ...
第3个回答  2011-01-18
接着你那句:加 group by 字段a having c 条件(比如>2)
相似回答