码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2015-08-19 19:27:56      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

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 );





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

标签:

原文地址:http://www.cnblogs.com/yilongm/p/4742875.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!