简体   繁体   English

如何通过分组或计数功能使用内部联接

[英]How to use an inner join with group by or count function

I'm use this SQL query to fetch data from a access database 我正在使用此SQL查询从Access数据库中获取数据

SELECT  Absence.date_, stages.name, course.course_name, student.name AS st_name 
FROM  (((Absence INNER JOIN  course ON Absence.course_id = course.ID) 
INNER JOIN       stages ON course.id_stage = stages.ID) 
INNER JOIN     student ON Absence.student_id = student.ID AND stages.ID = student.stage_id`

when i wont to group the field or get count of field it show to me this error message 当我不会将字段分组或获取字段计数时,它会向我显示此错误消息

Tried to execute a query that does not include the specified expression 'Absence.date_' as part of an aggregate function 试图执行不包含指定表达式“ Absence.date_”作为聚合函数一部分的查询

how can i get number of row ,distinct it by st_name , and group by stage name or course name 如何获取行数,按st_name区分行号以及按阶段名称或课程名称分组

For getting the count you should use 为了获得计数,您应该使用

SELECT  Absence.date_, stages.name, course.course_name, student.name AS st_name, count(*) 
FROM  (((Absence INNER JOIN  course ON Absence.course_id = course.ID) 
INNER JOIN       stages ON course.id_stage = stages.ID) 
INNER JOIN     student ON Absence.student_id = student.ID AND stages.ID = student.stage_id
GROUP BY Absence.date_, stages.name, course.course_name, student.name

What table contains the field "Absence.date_" ? 哪个表包含“ Absence.date_”字段? And you do not need to specify "Inner" (or Natural for that matter). 并且您无需指定“内部”(或自然)。

When using aggregate functions (Count(), Sum(), ect) your group by must contain the same list of fields as your select statement. 使用聚合函数(Count(),Sum()等)时,分组依据必须包含与select语句相同的字段列表。 And that error states that either Absence.date_ does not exist as a field in any of the tables in the from clause. 并且该错误表明Absence.date_在from子句的任何表中都不作为字段存在。 But, you have no Group By statement in your query. 但是,您的查询中没有Group By语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM