zoukankan      html  css  js  c++  java
  • 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 );





  • 相关阅读:
    django报错信息解决办法
    postgresql数据库的基本使用
    Ubuntu 18.04中安装PostgreSQL(PSQL)
    Ubuntu 18.04 安装 PyCharm
    在Ubuntu中安装Docker和docker的使用
    ubuntu怎么切换到root用户,切换到root账号方法
    centos7彻底卸载mysql和通过yum安装mysql
    django运行时报错处理方法
    Django 查询时间段 时间搜索 过滤
    View Controller Programming Guide for iOS---(四)---Creating Custom Content View Controllers
  • 原文地址:https://www.cnblogs.com/yilongm/p/4742875.html
Copyright © 2011-2022 走看看