计数列的where子句

时间:2018-06-13 11:27:11

标签: sql mysqli

我有两个表pdc和class

select roll_no as roll,sum(pdc.amount) as amount,count(amount) as given,
       stu_profile.name,f_name,scholarship,class_id,batch_id,statuss 
from stu_profile left join 
     pdc 
     on pdc.roll=stu_profile.roll_no 
where 1 and class_id!='' and given=0 
group by roll

我想要一个计数(金额)栏的条件

2 个答案:

答案 0 :(得分:2)

使用having子句

 . . .
having count(amount) = 0;

答案 1 :(得分:0)

然后回答你的问题是having条款。但是,您的查询格式错误,无法在大多数数据库中运行,包括更新版本的MySQL。

解决方案很简单:在聚合查询中,SELECT中的唯一列应该是GROUP BY密钥或聚合函数的参数。

让我假设你想要:

select p.roll_no as roll, sum(pdc.amount) as amount,
       count(pdc.amount) as given,
       p.name, p.f_name, p.scholarship, p.class_id, p.batch_id, p.statuss
from stu_profile p left join 
     pdc 
     on pdc.roll = p.roll_no 
where 1 and class_id <> '' 
group by roll, p.name, p.f_name, p.scholarship, p.class_id, p.batch_id, p.statuss

然后你可以添加如下内容:

having given = 0