我在这里想要的是获得哪个学科已经有 4 个教室,如果它超过了发出通知并返回 null,并且有效。但是当我插入其他主题“2”时,代码会发出相同的通知,但我这个主题只有 1 个课堂。我知道我正在使用“ HAVING COUNT(cod_classroom) = 4
”并且代码只能得到该主题已经有 4 个教室的内容。我试图只使用这个:“ SELECT DISTINCT cod_subject,COUNT(cod_classroom) AS CountOf FROM registration_subject GROUP BY cod_subject
”但我不知道如何检查更多的值。
我希望你们都明白我想要什么,我尽力了=)提前谢谢大家
CREATE OR REPLACE FUNCTION quantas()
RETURNS trigger AS
$BODY$declare qtd record;
begin
SELECT * INTO qtd FROM (SELECT DISTINCT cod_subject,COUNT(cod_classroom) AS CountOf FROM registration_subject
GROUP BY cod_subject HAVING COUNT(cod_classroom) = 4) AS total;
if found then
raise notice 'This subject has already 4 classroom';
return null;
end if;
end;$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION qtd()
OWNER TO postgres;