SQL Lite平均分数

时间:2017-03-27 09:56:19

标签: java sql sqlite

PreStatement = myConnection.prepareStatement(""
                        + "SELECT Game1.AVG(Score),Students.FirstName,Students.LastName "
                        + "FROM Game1 " 
                        + "INNER JOIN Students " 
                        + "ON Game1.StudentID = Students.StudentID " 
                        + "ORDER BY Students.FirstName ASC;");

这项工作。 (错误肯定是AVG评分部分)
但这确实如此。

PreStatement = myConnection.prepareStatement(""
                        + "SELECT AVG(Score),
                        + "FROM Game1 " 

我做错了什么,或者我要做两个陈述来获得我的平均分数。

2 个答案:

答案 0 :(得分:2)

试试这个。

PreStatement = myConnection.prepareStatement(""
                        + "SELECT AVG(Game1.Score),Students.FirstName,Students.LastName "
                        + "FROM Game1 " 
                        + "INNER JOIN Students " 
                        + "ON Game1.StudentID = Students.StudentID " 
                        + "ORDER BY Students.FirstName ASC;");

答案 1 :(得分:0)

如果您想要每个学生的平均值,那么您应该使用GROUP BY

SELECT AVG(g.Score), s.FirstName, s.LastName
FROM Game1 g INNER JOIN
     Students s
     ON g.StudentID = s.StudentID 
GROUP BY s.FirstName, s.LastName
ORDER BY s.FirstName ASC;

表别名也使查询更容易编写和阅读。