簡體   English   中英

計數列上的 Where 子句

[英]Where clause on count column

我有兩個表 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

我想要一個計數(數量)列的條件

改用having子句

 . . .
having count(amount) = 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM