zoukankan      html  css  js  c++  java
  • mysql 去重

    https://blog.csdn.net/eagle89/article/details/90901755

    假设有一个表user,字段分别有id–nick_name–password–email–phone,分情况如下(注意删除多余记录时要创建临时表,不然会报错):

    一、单字段(nick_name)

    1、查出所有有重复记录的所有记录

    select * from user where nick_name in

         (select nick_name from user group by nick_name having count(nick_name)>1);

    2、查出有重复记录的各个记录组中id最大的记录

    select * from user where id in (select max(id) from user group by nick_name having count(nick_name)>1);

    3、查出多余的记录,不查出id最小的记录

    select * from user where nick_name in

         (select nick_name from user group by nick_name having count(nick_name)>1)

    and id not in

         (select min(id) from user group by nick_name having count(nick_name)>1);

    4、删除多余的重复记录,只保留id最小的记录

    delete from user where nick_name in

         (select nick_name from

              (select nick_name from user group by nick_name having count(nick_name)>1) as tmp1)

    and id not in

          (select id from

              (select min(id) from user group by nick_name having count(nick_name)>1) as tmp2);

    二、多字段(nick_name,password)

    1、查出所有有重复记录的记录

    select * from user where (nick_name,password) in

         (select nick_name,password from user group by nick_name,password where having count(nick_name)>1);

    2、查出有重复记录的各个记录组中id最大的记录

    select * from user where id in

         (select max(id) from user group by nick_name,password where having count(nick_name)>1);

    3、查出各个重复记录组中多余的记录数据,不查出id最小的一条

    select * from user where (nick_name,password) in

         (select nick_name,password from user group by nick_name,password having count(nick_name)>1)

    and id not in

         (select min(id) from user group by nick_name,password having count(nick_name)>1);

    4、删除多余的重复记录,只保留id最小的记录

    delete from user where (nick_name,password) in

         (select nick_name,password from

              (select nick_name,password from user group by nick_name,password having count(nick_name)>1) as tmp1)

    and id not in

         (select id from

              (select min(id) id from user group by nick_name,password having count(nick_name)>1) as tmp2);

  • 相关阅读:
    Zend框架2入门(二) (转)
    Zend框架2入门(一) (转)
    PHP Strict standards:Declaration of … should be compatible with that of…(转)
    ::符号
    mysql查询今天,昨天,近7天,近30天,本月,上一月数据的方法(转)
    php 获取今日、昨日、上周、本月的起始时间戳和结束时间戳的方法(转)
    PHP5.4新特性(转)
    PHP5.4的变化关注---What has changed in PHP 5.4.x(转)
    关于PHP的curl开启问题 (转)
    安装apache重启的时候,报错端口被占用,错误1
  • 原文地址:https://www.cnblogs.com/zonglonglong/p/13678504.html
Copyright © 2011-2022 走看看