搜索
您的当前位置:首页正文

MySQL去重的方法整理

2023-11-21 来源:知库网

MySQL去重的方法整理

【初级】有极少的重复行

使用distinct查出来,然后手动一行一行删除。

【中级】按照单个字段的重复去重

例如:对id字段去重

使用方法:获取id的重复字段的值,利用相同id字段所在的行中,比较出数据不同的字段,删除 除了最小(或最大)的字段所在的该行之外的所有重复的行。一般使用主键来比较,因为主键的值一定是唯一值,绝对不相同。

id name1 a1 b2 c2 a3 c

结果:

id name1 a 2 a

操作:

delete from a_tmp where id in (select * from (select b.id from a_tmp b group by b.id having count(b.id) >1) bb) and name not in (select * from (select min(a.name) from a_tmp a GROUP BY a.id having count(a.id) >1) aa);

注意:

上述加粗并绿色的字,必须加别名,必须使用select * from (……)这样的格式,否则会报错:

[Err] 1093 - You can't specify target table 'a_tmp' for update in FROM clause

【高级】按多个字段的重复来去重

例如:对id,name相同的去重,即:对id,name都相同的算作重复行,对id相同而name不同的算作不重复行

使用方法:和单个字段相似,一般使用主键来比较,因为主键的值一定是唯一值。

id name rowid1 a 11 a 21 b 32 b 42 b 53 c 63 d 7

结果:

id name rowid1 a 1 1 b 32 b 43 c 63 d 7

操作:

第一种:

delete from a_tmp where (id,name) in (select * from (select b.id,b.name from a_tmp b group by b.id,b.name having count(b.id) >1) bb) and rowid not in (select * from (select min(a.rowid) from a_tmp a group by a.id,a.name having count(a.id) >1) aa);

第二种:

将id和name字段的值连接起来插入到临时表中b_tmp,这样便可以使用【中级】的单字段的判断删除方法。

#将两字段连接的值,a_tmp表中唯一值的字段插入b_tmp表

insert into b_tmp select concat(id,name),rowid from a_tmp;#查出需要留下来的行select id_name,max(rowid) from b_tmp group by id_name having count(id_name)>1;#使用【中级】的方法,或存储过程完成去重的工作

 【终极】每行都有两份一样的数据

例如:

使用方法:对于整行的数据都一样,是没办法使用SQL语句删除的,因为没有可以使用的条件限制来留下一行删除所有与其相同的行。没有不同的字段可以自己创造不同的字段,即:添加一个字段,设为自增长,并设为主键,它会自动添加上值。

id name1 a1 a1 b1 b2 c2 c3 c3 c

结果:

id name rowid1 a 11 b 32 c 53 c 7

操作:

添加一个自增长的字段,并暂时设为主键。

使用上面【中级】和【高级】的方法操作。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

您可能感兴趣的文章:
  • 一条sql语句完成MySQL去重留一
  • mysql 开发技巧之JOIN 更新和数据查重/去重
  • Mysql删除重复的数据 Mysql数据去重复
  • mysql去重的两种方法详解及实例代码
  • MySQL数据表合并去重的简单实现方法
  • mysql自联去重的一些笔记记录
  • 小编还为您整理了以下内容,可能对您也有帮助:

    mysql怎么去除重复数据

    MySQL 删除重复数据

    有些 MySQL 数据表中可能存在重复的记录,有些情况我们允许重复数据的存在,但有时候我们也需要删除这些重复的数据。

    本章节我们将为大家介绍如何防止数据表出现重复数据及如何删除数据表中的重复数据。

    删除重复数据

    如果你想删除数据表中的重复数据,你可以使用以下的SQL语句:

    from 树懒学堂 - 一站式数据知识平台

    当然你也可以在数据表中添加 INDEX(索引) 和 PRIMAY KEY(主键)这种简单的方法来删除表中的重复记录。方法如下:

    mysql怎么去除重复数据

    MySQL 删除重复数据

    有些 MySQL 数据表中可能存在重复的记录,有些情况我们允许重复数据的存在,但有时候我们也需要删除这些重复的数据。

    本章节我们将为大家介绍如何防止数据表出现重复数据及如何删除数据表中的重复数据。

    删除重复数据

    如果你想删除数据表中的重复数据,你可以使用以下的SQL语句:

    from 树懒学堂 - 一站式数据知识平台

    当然你也可以在数据表中添加 INDEX(索引) 和 PRIMAY KEY(主键)这种简单的方法来删除表中的重复记录。方法如下:

    mysql删除重复数据,保留一条

    mysql数据表中有多条重复数据记录,现在想删除删除部分重复数据,保留最后一条更新或者插入的数据。

    以学生表为例,我们创建一个简单的数据表来做实验:

    往表里面插入一些实验数据:

    我们可以根据分组查询先将重复数据查询出来,同时也可以获取到最后的更新时间,然后再与原表联表查询小于最大时间的数据,将查询出来的数据删除。

    ------先来慢慢消化-------

    在做删除前,我们可以先看看有哪些数据是有重复的:

    可以看到张三,李四,王五的数据是有重复的,赵六没有重复,下面我们查找最后更新的记录。

    可以看到,最后更新的数据为15:57:46的记录没有在结果中。

    可以看到重复记录已经被清理掉。

    假如有两行记录是完全一样的,这个方法就不可行了,往表里面在跑一次数据插入:

    执行删除计划:

    创建一个临时表存放最后插入的一条数据(包含重复与没有重复的),然后清空原表,再将临时表的数据复制到原表中,最后把临时表删除。

    这个很好理解,相当于ctrl+c,ctrl+v的操作,数据表如下:

    这样数据去重就完成了,需要注意的是, 如果表数据量很大,注意在group by 里面的字段建立索引,同时,生产环境注意好先进行数据备份操作 。

    mysql 对某几个字段去重

    -------------------部分字段重复---------------------
    --1.加索引的方式
    create table test_2(id int,value int);
    insert test_2 select 1,2 union all select 1,3 union all select 2,3;
    Alter IGNORE table test_2 add primary key(id);
    select * from test_2;
    +----+-------+
    | id | value |
    +----+-------+
    | 1 | 2 |
    | 2 | 3 |
    +----+-------+
    我们可以看到 1 3 这条记录消失了
    我们这里也可以使用Unique约束 因为有可能列中有NULL值,但是这里NULL就可以多个了..
    --2.联合表删除
    create table test_2(id int,value int);
    insert test_2 select 1,2 union all select 1,3 union all select 2,3;
    delete A from test_2 a join (select MAX(value) as v ,ID from test_2 group by id) b
    on a.id=b.id and a.value<>b.v;
    select * from test_2;
    +------+-------+
    | id | value |
    +------+-------+
    | 1 | 3 |
    | 2 | 3 |
    +------+-------+
    --3.使用Increment_auto也可以就是上面全部字段去重的第二个方法
    --4.容易错误的方法
    --有些朋友可能会想到子查询的方法,我们来试验一下
    create table test_2(id int,value int);
    insert test_2 select 1,2 union all select 1,3 union all select 2,3;
    delete a from test_2 a where exists(select * from test_2 where a.id=id and a.value<value);
    /*ERROR 1093 (HY000): You can't specify target table 'a' for update in FROM clause*/

    目前,您不能从一个表中删除,同时又在子查询中从同一个表中选择。

    mysql 对某几个字段去重

    -------------------部分字段重复---------------------
    --1.加索引的方式
    create table test_2(id int,value int);
    insert test_2 select 1,2 union all select 1,3 union all select 2,3;
    Alter IGNORE table test_2 add primary key(id);
    select * from test_2;
    +----+-------+
    | id | value |
    +----+-------+
    | 1 | 2 |
    | 2 | 3 |
    +----+-------+
    我们可以看到 1 3 这条记录消失了
    我们这里也可以使用Unique约束 因为有可能列中有NULL值,但是这里NULL就可以多个了..
    --2.联合表删除
    create table test_2(id int,value int);
    insert test_2 select 1,2 union all select 1,3 union all select 2,3;
    delete A from test_2 a join (select MAX(value) as v ,ID from test_2 group by id) b
    on a.id=b.id and a.value<>b.v;
    select * from test_2;
    +------+-------+
    | id | value |
    +------+-------+
    | 1 | 3 |
    | 2 | 3 |
    +------+-------+
    --3.使用Increment_auto也可以就是上面全部字段去重的第二个方法
    --4.容易错误的方法
    --有些朋友可能会想到子查询的方法,我们来试验一下
    create table test_2(id int,value int);
    insert test_2 select 1,2 union all select 1,3 union all select 2,3;
    delete a from test_2 a where exists(select * from test_2 where a.id=id and a.value<value);
    /*ERROR 1093 (HY000): You can't specify target table 'a' for update in FROM clause*/

    目前,您不能从一个表中删除,同时又在子查询中从同一个表中选择。

    mysql查询去掉重复数据

    可以利用distinct关键字对需要处理的字段进行去重

    使用group by关键字对去重数据进行去重查询,针对某个字段查询,直接group by 这个字段

    在group by 的基础上 也可以使用 having 对查询结果进行二次筛选

    mysql查询去掉重复数据

    可以利用distinct关键字对需要处理的字段进行去重

    使用group by关键字对去重数据进行去重查询,针对某个字段查询,直接group by 这个字段

    在group by 的基础上 也可以使用 having 对查询结果进行二次筛选

    Mysql根据一张表俩个字段删除重复数据

    如果你需要的是删除数据库中的数据:
    delete from table a
    where (a.user_id,a.tw_id) in (select user_id,tw_id from vitae group by user_id,tw_id having count(*) > 1)
    and rowid not in (select min(rowid) from vitae group by user_id,tw_id having count(*)>1)
    如果只是查出是去重:
    select distinct user_id,tw_id from table

    Mysql根据一张表俩个字段删除重复数据

    如果你需要的是删除数据库中的数据:
    delete from table a
    where (a.user_id,a.tw_id) in (select user_id,tw_id from vitae group by user_id,tw_id having count(*) > 1)
    and rowid not in (select min(rowid) from vitae group by user_id,tw_id having count(*)>1)
    如果只是查出是去重:
    select distinct user_id,tw_id from table

    mysql 根据两个字段值查询时如何去除重复数据

    假设表名为test:

    select * from (select *, concat(name,code) as __f from test order by date desc) __t group by __f;

    mysql查出n条数据,其中有四条数据两两重复,还有其他的也是这样,如何去重?

    SELECT * FROM(

    select * from customer where user=(

    SELECT source_user from customer WHERE user='admin') UNION ALL select * from customer where user=(

    select source_user from customer where user=(

    SELECT source_user from customer WHERE user='admin')) union ALL select * from customer where user=(

    select source_user from customer where user=(

    select source_user from customer where user=(

    SELECT source_user from customer WHERE user='admin'))) UNION ALL select * from customer where source_user=(/*我的上线的上线的user*/

    select user from customer where user=(

    select source_user from customer where user=(

    SELECT source_user from customer WHERE user='admin'))) union all select * from customer where source_user=(/*我的上线的上线的上线user*/

    select user from customer where user=(

    select source_user from customer where user=(

    select source_user from customer where user=(

    SELECT source_user from customer WHERE user='admin'))))) as alias group by user;

    本文如未解决您的问题请添加抖音号:51dongshi(抖音搜索懂视),直接咨询即可。

    上一篇:小孩名字叫什么好

    下一篇:

    热门图文

    • JS判断浏览器类型与版本的实现代码_javascript技巧

      js判断浏览器类型及版本在网站前端开发中,浏览器兼容性是前端开发框架要解决的第一个问题,要解决兼容性问题就得首先准确判断出浏览器的类型及其版本,而判断浏览器的版本一般只能通过分析浏览器的userAgent才能知道.各种浏览器的userAgent:IE各个版本典型的userAgent如下 Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2) Mozi

    • js事件(Event)知识整理_javascript技巧

      JS的event对象Event属性和方法:1. type:事件的类型,如onlick中的click;2. srcElement/target:事件源,就是发生事件的元素;3. button:声明被按下的鼠标键,整数,1代表左键,2代表右键,4代表中键,如果按下多个键,酒把这些值加起来,所以3就代表左右键同时按下;(firefox中 0代表左键,1代表中间键,2代表右键)4. clientX/clientY:事件发生的时候,鼠标相对于浏览器窗口可视文档区域的左上角的位置;(在DOM标准中,这两个属

    • 台式电脑主机与显示器连接的线叫什么

      电脑连接显示器的线叫vga,hdmi,dp线。hdmi就是高清多媒体接口。这是一种全数字化视频和声音发送的接口,可以用来发送未压缩的音频及视频信号等。HDMI可用于机顶盒、DVD播放机、个人计算机、电视、游戏主机、综合扩大机、数字音响与电视机等设备。HDMI可以同时发送音频和视频信号,由于音频和视频信号采用同一条线材,大大简化系统线路的安装难度。HDMI是被设计来取代较旧的模拟信号影音发送接口如SCART或RCA等端子的。它支持各类电视与计算机视频格式,包括SDTV、HDTV视频画面,再加上多声

    • jquery聚焦文本框与扩展文本框聚焦方法_jquery

      需要准备的材料分别有:电脑、html编辑器、浏览器。1、首先,打开html编辑器,新建html文件,例如:index.html,并引入jquery。2、其次,在index.html中的&lt;script&gt;标签,输入jquery代码:$(&#39;input&#39;).focus();。3、浏览器运行index.html页面,此时用jquery成功获取了input输入框的焦点。

    • javascript中方便增删改cookie的一个类_javascript技巧

      function SetCookie(name,value)//两个参数,一个是cookie的名子,一个是值{ var Days = 30; //此 cookie 将被保存 30 天 var exp = new Date(); //new Date(&quot;December 31, 9998&quot;); exp.setTime(exp.getTime() + Days*24*60*60*1000); document.cookie = name + &qu

    Top