当我们使用以下字符串然后产生错误,帮助我解决

时间:2012-08-23 13:34:13

标签: sql

selectString = @"SELECT StudentRegistrationInformation.registrationNo,
                        StudentRegistrationInformation.firstName,
                        StudentRegistrationInformation.lastName,
                        StudentPayment.subjectName,
                        sum(StudentPayment.feeReceived) AS totalReceivedFee
                 FROM StudentRegistrationInformation
                 INNER JOIN StudentPayment 
                     ON StudentPayment.registrationNo =
                            StudentRegistrationInformation.registrationNo";

错误:

  

列'StudentRegistrationInformation.registrationNo'无效   选择列表,因为它不包含在聚合中   函数或GROUP BY子句。

3 个答案:

答案 0 :(得分:2)

您在查询中使用了聚合函数。因此,您的查询需要知道如何对其余结果进行分组。就像在查询结尾添加以下内容一样简单:

GROUP BY StudentRegistrationInformation.registrationNo,
         StudentRegistrationInformation.firstName,
         StudentRegistrationInformation.lastName,
         StudentPayment.subjectName

答案 1 :(得分:0)

您必须在查询结尾处添加Group By,而不选择聚合函数

SELECT StudentRegistrationInformation.registrationNo,
                                            StudentRegistrationInformation.firstName,
                                            StudentRegistrationInformation.lastName,
                                            StudentPayment.subjectName,
                                            sum(StudentPayment.feeReceived) AS        totalReceivedFee
                                       FROM StudentRegistrationInformation
                                            INNER JOIN StudentPayment ON
                                            StudentPayment.registrationNo=StudentRegistrationInformation.registrationNo

GROUP BY tudentRegistrationInformation.registrationNo,
                                            StudentRegistrationInformation.firstName,
                                            StudentRegistrationInformation.lastName,
                                            StudentPayment.subjectName

答案 2 :(得分:0)

因为您使用的是聚合函数“sum”,所以需要添加

GROUP BY StudentRegistrationInformation.registrationNo,
         StudentRegistrationInformation.firstName,
         StudentRegistrationInformation.lastName,
         StudentPayment.subjectName

到您的SQL字符串。