1

我的数据库中有数据

ID    UserID   CourseName   Grade                                  
 1      8        CourseA      75
 2      8        CourseA      102 
 3      8        CourseA      115
 4      8        CourseA      111
 5      8        CourseA      112
 6      8        CourseB      60
 7      8        CourseB      45
 8      8        CourseB      58
 9      8        CourseB      60
 10     8        CourseB      74
 11     8        CourseC      130
 12     8        CourseC      140
 13     8        CourseC      133 
 14     8        CourseC      135

我需要一门课程的最后更新成绩,例如

`8     CourseA   112    
 8     CourseB   74
 8     CourseC   135`

任何人都可以为此建议我一个 sql 查询。

4

4 回答 4

0
select * from Course a 
where id=(select max(b.id) from Course b where b.CourseName=a.CourseName); 

之后,您可以按 ID、CourseName 对结果进行分组

未测试!

于 2012-07-10T13:33:16.210 回答
0

好的,更新我的答案:

知道了:

SELECT UserID, CourseName, Grade
FROM yourtable
WHERE UserID = '8'
AND ID
IN (
    SELECT MAX( ID ) 
    FROM yourtable
    GROUP BY CourseName
)
于 2012-07-10T13:19:47.590 回答
0

您可以添加另一列修改日期

alter table tableName add column modifiedDate DEFAULT NOW() ON UPDATE NOW()

然后你会得到课程的最后更新成绩

 select * from tableName where course ='CourseA' 
 order by modifiedDate desc limit 1
于 2012-07-10T13:21:33.103 回答
0

SELECT * FROM Table ORDER BY id DESC LIMIT 1;

于 2020-04-02T06:07:18.897 回答