我有两个表格Affaire(ID, Obj, ID_TA ...)
和TypeAffaire(ID_TA, Label)
,我想计算Affaire中每个ID_TA
的数量,如果label
则显示ID_TA
,我试过这个,但它不起作用:
SELECT A.ID_TA
,T.Label
,count(*) AS TotalAmount
FROM Affaire AS A
LEFT JOIN TypeAffaire AS T ON T.ID_TA = A.TA
GROUP BY A.ID_TA;
它说无法找到标签
答案 0 :(得分:1)
您还需要按T.Label
进行分组。 select
子句中的所有非聚合字段也必须位于group by
子句中。
答案 1 :(得分:1)
在Group By
试试这个
SELECT A.ID_TA
,T.Label
,count(*) AS TotalAmount
FROM Affaire AS A
LEFT JOIN TypeAffaire AS T ON T.ID_TA = A.TA
GROUP BY A.ID_TA,T.Label;
答案 2 :(得分:0)
您需要在group by子句中插入T.Label,并更正ON T.ID_TA = A.TA in to ON T.ID_TA = A.ID_TA
SELECT A.ID_TA ,T.Label ,count(*) AS TotalAmount
FROM Affaire AS A
LEFT JOIN TypeAffaire AS T ON T.ID_TA = A.ID_TA
GROUP BY A.ID_TA, T.Label;