student_score表如下:
id | name | score |
1 | 张三 | 59 |
2 | 张三 | 90 |
3 | 李四 | 99 |
4 | 王五 | 60 |
表中张三的单元闯关成绩有2次,只保留最大值,其他删除,sql语句如下:
delete from student_score where id not in ( select a.nid from (select id as nid,name,max(score) as max_score from student_score group by name) as a);
执行后,table中数据如下:
id | name | score |
2 | 张三 | 90 |
3 | 李四 | 99 |
4 | 王五 | 60 |