日期:2014-05-18 浏览次数:20470 次
CREATE TABLE Student ( ID INT IDENTITY(1,1) PRIMARY KEY, 学号 VARCHAR(40), 姓名 VARCHAR(40), 成绩 FLOAT ) INSERT INTO Student VALUES('20110301','张三',82) INSERT INTO Student VALUES('20110302','李四',85) INSERT INTO Student VALUES('20110303','王五',86) INSERT INTO Student VALUES('20110304','马六',87) INSERT INTO Student VALUES('20110305','钱七',82) INSERT INTO Student VALUES('20110301','张三',85) INSERT INTO Student VALUES('20110303','王五',85) SELECT * FROM Student SELECT * FROM Student WHERE ID IN (SELECT MIN(ID) FROM Student GROUP BY 学号) /* ID 学号 姓名 成绩 ----------- ---------------------------------------- ---------------------------------------- ----------------------------------------------------- 1 20110301 张三 82.0 2 20110302 李四 85.0 3 20110303 王五 86.0 4 20110304 马六 87.0 5 20110305 钱七 82.0 (所影响的行数为 5 行) --DELETE FROM Student WHERE ID NOT IN(SELECT MIN(ID) FROM Student GROUP BY 学号)//删掉重复的 */
------解决方案--------------------
数据有点出入,纠正一下
CREATE TABLE Student ( ID INT IDENTITY(1,1) PRIMARY KEY, 学号 VARCHAR(40), 姓名 VARCHAR(40), 成绩 FLOAT ) INSERT INTO Student VALUES('20110301','张三',82) INSERT INTO Student VALUES('20110302','李四',84) INSERT INTO Student VALUES('20110303','王五',85) INSERT INTO Student VALUES('20110304','马六',86) INSERT INTO Student VALUES('20110305','钱七',87) INSERT INTO Student VALUES('20110301','张三',82) INSERT INTO Student VALUES('20110303','王五',85) SELECT * FROM Student SELECT * FROM Student WHERE ID IN (SELECT MIN(ID) FROM Student GROUP BY 学号) --DELETE FROM Student WHERE ID NOT IN(SELECT MIN(ID) FROM Student GROUP BY 学号)这个也行 /* ID 学号 姓名 成绩 ----------- ---------------------------------------- ---------------------------------------- --------------------- 1 20110301 张三 82.0 2 20110302 李四 84.0 3 20110303