简单问题 删除记录
创建了一个表名字是Table1,其中记录的重复类似如下:
a1, b1, c1, d1, 4
a1, b1, c12, d12, 5
a2, b2, c2, d2, 7
a3, b3, c3, d3, 15
a3, b3, c31 d31, 17
…
表的记录是这样的
1、前两列重复的,重复时,重复的记录只重复一次,比如,第1,2条记录,第3,4条记录;
2、也有不重复的记录,比如第3条记录;
3、现在我想删除重复的记录的一个,保留第5列最大的那个;
上面的表删除后的效果是这样的
a1, b1, c12, d12, 5
a2, b2, c2, d2, 7
a3, b3, c31 d31, 17
请问如何快速删除?
我用的是Mysql。
在此声明一下,本人sql语句不是很熟,回答是麻烦详细一点。谢谢了。
-
SQL code
mysql@stat1.db.test>alter table test add index idx_id_age_p(id,age,p); Query OK, 4 rows affected (0.03 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql@stat1.db.test>select id,age,max(p) from test group by id,age; +------+------+--------+ | id | age | max(p) | +------+------+--------+ | 1 | 1 | 2 | | 2 | 2 | 4 | +------+------+--------+ 2 rows in set (0.02 sec) mysql@stat1.db.test>delete from test where (id,age,p) not in (select * from (select id,age,max(p) as p from test group by id,age) as tmp); Query OK, 2 rows affected (0.02 sec) mysql@stat1.db.test>select * from test; +------+------+------+ | id | age | p | +------+------+------+ | 1 | 1 | 2 | | 2 | 2 | 4 | +------+------+------+ 2 rows in set (0.00 sec)