我的数据库中有数据
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查询。
答案 0 :(得分:0)
好的,更新我的回答:
知道了:
SELECT UserID, CourseName, Grade
FROM yourtable
WHERE UserID = '8'
AND ID
IN (
SELECT MAX( ID )
FROM yourtable
GROUP BY CourseName
)
答案 1 :(得分:0)
您可以添加其他列 修改日期
alter table tableName add column modifiedDate DEFAULT NOW() ON UPDATE NOW()
然后您获得课程的最后更新成绩
select * from tableName where course ='CourseA'
order by modifiedDate desc limit 1
答案 2 :(得分:0)
select * from Course a
where id=(select max(b.id) from Course b where b.CourseName=a.CourseName);
Aftwerwards您可以按ID,CourseName
对结果进行分组未经测试!
答案 3 :(得分:0)
SELECT * FROM Table ORDER BY ID DESC LIMIT 1;