<legend id='3reA0'><style id='3reA0'><dir id='3reA0'><q id='3reA0'></q></dir></style></legend>
  • <small id='3reA0'></small><noframes id='3reA0'>

    1. <tfoot id='3reA0'></tfoot>
    2. <i id='3reA0'><tr id='3reA0'><dt id='3reA0'><q id='3reA0'><span id='3reA0'><b id='3reA0'><form id='3reA0'><ins id='3reA0'></ins><ul id='3reA0'></ul><sub id='3reA0'></sub></form><legend id='3reA0'></legend><bdo id='3reA0'><pre id='3reA0'><center id='3reA0'></center></pre></bdo></b><th id='3reA0'></th></span></q></dt></tr></i><div id='3reA0'><tfoot id='3reA0'></tfoot><dl id='3reA0'><fieldset id='3reA0'></fieldset></dl></div>

      • <bdo id='3reA0'></bdo><ul id='3reA0'></ul>

        删除除 MySQL 中的一行之外的所有重复行?

        时间:2023-08-20
        <i id='eR3lz'><tr id='eR3lz'><dt id='eR3lz'><q id='eR3lz'><span id='eR3lz'><b id='eR3lz'><form id='eR3lz'><ins id='eR3lz'></ins><ul id='eR3lz'></ul><sub id='eR3lz'></sub></form><legend id='eR3lz'></legend><bdo id='eR3lz'><pre id='eR3lz'><center id='eR3lz'></center></pre></bdo></b><th id='eR3lz'></th></span></q></dt></tr></i><div id='eR3lz'><tfoot id='eR3lz'></tfoot><dl id='eR3lz'><fieldset id='eR3lz'></fieldset></dl></div>
          <bdo id='eR3lz'></bdo><ul id='eR3lz'></ul>

          <legend id='eR3lz'><style id='eR3lz'><dir id='eR3lz'><q id='eR3lz'></q></dir></style></legend>

            1. <tfoot id='eR3lz'></tfoot>
            2. <small id='eR3lz'></small><noframes id='eR3lz'>

                <tbody id='eR3lz'></tbody>

                  本文介绍了删除除 MySQL 中的一行之外的所有重复行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如何从 MySQL 表中删除所有重复数据?

                  How would I delete all duplicate data from a MySQL Table?

                  例如,使用以下数据:

                  SELECT * FROM names;
                  
                  +----+--------+
                  | id | name   |
                  +----+--------+
                  | 1  | google |
                  | 2  | yahoo  |
                  | 3  | msn    |
                  | 4  | google |
                  | 5  | google |
                  | 6  | yahoo  |
                  +----+--------+
                  

                  如果是 SELECT 查询,我会使用 SELECT DISTINCT name FROM names;.

                  I would use SELECT DISTINCT name FROM names; if it were a SELECT query.

                  我将如何使用 DELETE 执行此操作以仅删除重复项并仅保留每个记录?

                  How would I do this with DELETE to only remove duplicates and keep just one record of each?

                  推荐答案

                  编辑器警告:此解决方案计算效率低下,可能会导致大表的连接中断.

                  注意 - 您需要首先在您的表的测试副本上执行此操作!

                  NB - You need to do this first on a test copy of your table!

                  当我这样做时,我发现除非我还包含了 AND n1.id <>n2.id,它删除了表中的每一行.

                  When I did it, I found that unless I also included AND n1.id <> n2.id, it deleted every row in the table.

                  1. 如果要保留 id 值最低的行:

                  DELETE n1 FROM names n1, names n2 WHERE n1.id > n2.id AND n1.name = n2.name
                  

                • 如果要保留 id 值最高的行:

                  DELETE n1 FROM names n1, names n2 WHERE n1.id < n2.id AND n1.name = n2.name
                  

                • 我在 MySQL 5.1 中使用过这种方法

                  I used this method in MySQL 5.1

                  不确定其他版本.

                  更新:由于人们在谷歌上搜索删除重复项最终会出现在这里
                  尽管 OP 的问题是关于 DELETE,但请注意使用 INSERTDISTINCT 会快得多.对于一个有 800 万行的数据库,下面的查询用了 13 分钟,而使用 DELETE 时,用了 2 个多小时还没有完成.

                  Update: Since people Googling for removing duplicates end up here
                  Although the OP's question is about DELETE, please be advised that using INSERT and DISTINCT is much faster. For a database with 8 million rows, the below query took 13 minutes, while using DELETE, it took more than 2 hours and yet didn't complete.

                  INSERT INTO tempTableName(cellId,attributeId,entityRowId,value)
                      SELECT DISTINCT cellId,attributeId,entityRowId,value
                      FROM tableName;
                  

                  这篇关于删除除 MySQL 中的一行之外的所有重复行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:我应该在 MySQL 中使用日期时间还是时间戳数据类 下一篇:如何设置 MySQL 的时区?

                  相关文章

                  最新文章

                  <i id='imUaS'><tr id='imUaS'><dt id='imUaS'><q id='imUaS'><span id='imUaS'><b id='imUaS'><form id='imUaS'><ins id='imUaS'></ins><ul id='imUaS'></ul><sub id='imUaS'></sub></form><legend id='imUaS'></legend><bdo id='imUaS'><pre id='imUaS'><center id='imUaS'></center></pre></bdo></b><th id='imUaS'></th></span></q></dt></tr></i><div id='imUaS'><tfoot id='imUaS'></tfoot><dl id='imUaS'><fieldset id='imUaS'></fieldset></dl></div>

                        <bdo id='imUaS'></bdo><ul id='imUaS'></ul>
                    1. <legend id='imUaS'><style id='imUaS'><dir id='imUaS'><q id='imUaS'></q></dir></style></legend>

                      <small id='imUaS'></small><noframes id='imUaS'>

                      <tfoot id='imUaS'></tfoot>