1.表结构:
CREATE TABLE testS(id INT PRIMARY KEY AUTO_INCREMENT,stu_no VARCHAR(20),stu_name VARCHAR(22) ,course_no INT,course_name VARCHAR(20),score INT)
2.题目:
删除除了自动编号不同,其他都相同的学生冗余信息。
3.sql:
DELETE FROM tests WHERE id NOT IN ( SELECT b.bid FROM (SELECT MIN(id) bid,stu_no,course_no,score FROM tests GROUP BY stu_no,course_no,score ) b )
分析:
1) SELECT MIN(id) bid,stu_no,course_no,score FROM tests GROUP BY stu_no,course_no,score
首先按除了id以外的字段分组,查询出最小的id
2) SELECT b.bid FROM (SELECT MIN(id) bid,stu_no,course_no,score FROM tests GROUP BY stu_no,course_no,score ) b
这一个是查询出1中的最小id
3) DELETE FROM tests WHERE id NOT IN ...
删除id不包含2中查询出的数据