MYSQL 当有两条重复数据时 保留一条

delete from test  where id in (select id from (select  max(id) as id,count(text) as count from test group by text having count >1 order by count desc) as tab )


测试代码


 INSERT IGNORE INTO test_1(text,text2) values ('1111','22222');
 INSERT IGNORE INTO test_1(text,text2) values ('1111','22222');
 INSERT IGNORE INTO test_1(text,text2) values ('1111','22222');
 INSERT IGNORE INTO test_1(text,text2) values ('1111','22222');
 INSERT IGNORE INTO test_1(text,text2) values ('1111','22222');
 INSERT IGNORE INTO test_1(text,text2) values ('1111','33333');
 INSERT IGNORE INTO test_1(text,text2) values ('1111','33333');
 INSERT IGNORE INTO test_1(text,text2) values ('1111','33333');
 INSERT IGNORE INTO test_1(text,text2) values ('1111','33333');
 INSERT IGNORE INTO test_1(text,text2) values ('1111','33333');
 INSERT IGNORE INTO test_1(text,text2) values ('1111','33333');
 INSERT IGNORE INTO test_1(text,text2) values ('1111','33333');

 INSERT IGNORE INTO test_1(text,text2) values ('22222','33333');
 INSERT IGNORE INTO test_1(text,text2) values ('22222','33333');
 INSERT IGNORE INTO test_1(text,text2) values ('22222','33333');
 INSERT IGNORE INTO test_1(text,text2) values ('22222','33333');
 INSERT IGNORE INTO test_1(text,text2) values ('22222','33333');
 INSERT IGNORE INTO test_1(text,text2) values ('22222','33333');


 REPLACE  INTO test(text) values ('1111')

delete from test

select * from test_1 where text='1111'
select * from test_1 where text='22222'

while 
delete from test_1  where id in (select id from (select  max(id) as id,count(text) as count from test_1 where text = '22222' group by text,text2 having count >1 order by count desc) as tab );

delete from test_1  where id in (select id from (select  max(id) as id,count(text) as count from test_1 where text = '22222' group by text,text2 having count >1 order by count desc) as tab );
delete from test_1  where id in (select id from (select  max(id) as id,count(text) as count from test_1 where text = '22222' group by text,text2 having count >1 order by count desc) as tab );
delete from test_1  where id in (select id from (select  max(id) as id,count(text) as count from test_1 where text = '22222' group by text,text2 having count >1 order by count desc) as tab );
delete from test_1  where id in (select id from (select  max(id) as id,count(text) as count from test_1 where text = '22222' group by text,text2 having count >1 order by count desc) as tab );
delete from test_1  where id in (select id from (select  max(id) as id,count(text) as count from test_1 where text = '22222' group by text,text2 having count >1 order by count desc) as tab );
delete from test_1  where id in (select id from (select  max(id) as id,count(text) as count from test_1 where text = '22222' group by text,text2 having count >1 order by count desc) as tab );
delete from test_1  where id in (select id from (select  max(id) as id,count(text) as count from test_1 where text = '22222' group by text,text2 having count >1 order by count desc) as tab );





posted on 2015-08-19 17:31  哈哈哈哈BBA  阅读(453)  评论(0编辑  收藏  举报

导航