请帮助我解决这个问题。
SELECT COUNT (name.IN) AS Total, name.St_Name, name.Class_Id,
teacher.Class_Id
FROM name
Left Join teacher ON name.Class_Id = teacher.Class_Id
WHERE name.IN = '1';
问题:我如何COUNT(name.IN)
总计name.Class_Id
和teacher.Class_Id
相同的学生人数。
答案 0 :(得分:1)
如果使用诸如COUNT或SUM或MAX的聚合函数?
然后,当您将其他字段添加到SELECT时,这些字段应位于GROUP BY中。
对于大多数数据库都是如此。但是MySql可能是该SQL92规则的例外。
在MariaDB中,如果使用ONLY_FULL_GROUP_BY
SQL_MODE,则不允许从GROUP BY中忽略那些字段。
所以尝试一下:
SELECT
COUNT(name.IN) AS Total,
COUNT(DISTINCT name.St_Name) AS TotalUniqueStudentNames,
COUNT(teacher.Class_id) AS TotalMatchingTeacherClass,
name.Class_Id
FROM name
LEFT JOIN teacher ON name.Class_Id = teacher.Class_id
WHERE name.IN = '1'
GROUP BY name.Class_Id
ORDER BY name.Class_Id
答案 1 :(得分:1)
一个SELECT语句子句,将查询结果分为几组行,通常是为了对每一组执行一个或多个聚合。 SELECT语句每组返回一行
SELECT (name.IN) AS Total, name.St_Name, name.Class_Id, teacher.Class_Id
FROM name
Left Join teacher ON name.Class_Id = teacher.Class_Id
WHERE name.IN = '1'
group by name.St_Name, name.Class_Id, teacher.Class_Id