首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > Mysql >

简单有关问题 删除记录

2012-06-05 
简单问题 删除记录创建了一个表名字是Table1,其中记录的重复类似如下:a1, b1, c1, d1, 4a1, b1, c12, d12,

简单问题 删除记录
创建了一个表名字是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: 0mysql@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) 

热点排行