zoukankan      html  css  js  c++  java
  • mysql删除重复数据只保留一条

    建表语句

    CREATE TABLE `student` (
        `id` BIGINT (20),
        `s_name` VARCHAR (765),
        `age` INT (2)
    ); 
    INSERT INTO `student` (`id`, `s_name`, `age`) VALUES('100','','15');
    INSERT INTO `student` (`id`, `s_name`, `age`) VALUES('111','','14');
    INSERT INTO `student` (`id`, `s_name`, `age`) VALUES('112','','12');
    INSERT INTO `student` (`id`, `s_name`, `age`) VALUES('122','','10');
    INSERT INTO `student` (`id`, `s_name`, `age`) VALUES('123','','13');
    INSERT INTO `student` (`id`, `s_name`, `age`) VALUES('124','','11');
    INSERT INTO `student` (`id`, `s_name`, `age`) VALUES('126','','11');

     在网上查到很多关于这道题的答案,但很多都是错的,比如

    delete from student 
    where s_name in (select s_name from student group by s_name having count(s_name) > 1) 
    and id not in (select min(id) from people group by s_name having count(s_name)>1) 

    这句话在MySQL里执行会报:

    You can't specify target table 'student' for update in FROM clause

    意思就是不能先select出同一表中的某些值,再update这个表(在同一语句中),即不能依据某字段值做判断再来更新某字段的值。

    解决方案就是用通过中间表来规避这个错误,sql语句如下:

    DELETE 
    FROM
      student 
    WHERE id NOT IN 
      (SELECT 
        a.id 
      FROM
        (SELECT 
          MIN(id) AS id 
        FROM
          student 
        GROUP BY s_name 
        HAVING COUNT(s_name) > 1) AS a) 

    但是这样写会有个问题,会把s_name没有重复的数据也删掉例如:

    INSERT INTO `student` (`id`, `s_name`, `age`) VALUES('122','张','10');这条数据

    很多答案也没有考虑到这个问题,把s_name没有重复的数据排除掉就行了。

    最后形成的语句如下:

    DELETE FROM  student WHERE id NOT IN 
      (SELECT  a.id   FROM
    (SELECT MIN(id) AS id FROM student GROUP BY s_name HAVING COUNT(s_name) > 1) AS a) AND s_name IN
    (SELECT b.s_name FROM (SELECT s_name FROM student GROUP BY s_name HAVING COUNT(s_name) > 1) AS b)

      

    或者是

    DELETE FROM student WHERE id NOT IN 
      (SELECT  a.id  FROM
        (SELECT  MIN(id) AS id  FROM  student  GROUP BY s_name ) AS a) 
  • 相关阅读:
    小账本软件设计之数据库设计模式构建
    基于JMeter的Quick Easy FTP Server性能测试
    构建之法 -源代码管理
    小账本APP——软件项目风险管理及解决办法案例
    基于python的Splash基本使用和负载均衡配置
    MQ初窥门径【面试必看的Kafka和RocketMQ存储区别】
    Apollo源码搭建调试看一文就够
    log4j2异步日志解读(二)AsyncLogger
    Disruptor源码解读
    高性能队列disruptor为什么这么快?
  • 原文地址:https://www.cnblogs.com/sqy123/p/10038559.html
Copyright © 2011-2022 走看看