Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p><strong>Editor warning: This solution is computationally inefficient and may bring down your connection for a large table.</strong></p> </blockquote> <p><strong>NB - You <em>need</em> to do this first on a <em>test copy</em> of your table!</strong></p> <p><strong>When I did it, I found that unless I also included <code>AND n1.id &lt;&gt; n2.id</code>, it deleted every row in the table.</strong></p> <ol> <li><p>If you want to keep the row with the lowest <code>id</code> value: </p> <pre class="lang-sql prettyprint-override"><code>DELETE n1 FROM names n1, names n2 WHERE n1.id &gt; n2.id AND n1.name = n2.name </code></pre></li> <li><p>If you want to keep the row with the highest <code>id</code> value:</p> <pre class="lang-sql prettyprint-override"><code>DELETE n1 FROM names n1, names n2 WHERE n1.id &lt; n2.id AND n1.name = n2.name </code></pre></li> </ol> <p>I used this method in MySQL 5.1</p> <p>Not sure about other versions. </p> <hr> <p><strong>Update: Since people Googling for removing duplicates end up here</strong><br> Although the OP's question is about <code>DELETE</code>, please be advised that using <code>INSERT</code> and <code>DISTINCT</code> is much faster. For a database with 8 million rows, the below query took 13 minutes, while using <code>DELETE</code>, it took more than 2 hours and yet didn't complete. </p> <pre><code>INSERT INTO tempTableName(cellId,attributeId,entityRowId,value) SELECT DISTINCT cellId,attributeId,entityRowId,value FROM tableName; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload