美文网首页
MYSQL 当有两条重复数据时 保留一条

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

作者: 逆向工程sudolong | 来源:发表于2019-08-19 12:23 被阅读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 当有两条重复数据时 保留一条

          本文链接:https://www.haomeiwen.com/subject/zrizjftx.html