日期:2014-05-16 浏览次数:20723 次
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)