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

你可能感兴趣的:(MYSQL 当有两条重复数据时 保留一条)