<?xml version="1.0" encoding="utf-8" ?>

<rss version="2.0" 
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:admin="http://webns.net/mvcb/"
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
   xmlns:wfw="http://wellformedweb.org/CommentAPI/"
   xmlns:content="http://purl.org/rss/1.0/modules/content/"
   >
<channel>
    <title>Webconsults Europe Consulting in PHP  Databases AJAX SEO WEB 20 Webdevelopment - Database</title>
    <link>http://webconsults.eu/</link>
    <description>PHP AJAX OOP SEO Training MySQL WEB 2 0 JSON Security OOP</description>
    <dc:language>en</dc:language>
    <generator>Serendipity 1.5.1 - http://www.s9y.org/</generator>
    <pubDate>Mon, 08 Feb 2010 13:50:38 GMT</pubDate>

    <image>
        <url>http://webconsults.eu/templates/default/img/s9y_banner_small.png</url>
        <title>RSS: Webconsults Europe Consulting in PHP  Databases AJAX SEO WEB 20 Webdevelopment - Database - PHP AJAX OOP SEO Training MySQL WEB 2 0 JSON Security OOP</title>
        <link>http://webconsults.eu/</link>
        <width>100</width>
        <height>21</height>
    </image>

<item>
    <title>Remove Duplicated Entries from MySQL Table</title>
    <link>http://webconsults.eu/archives/61-Remove-Duplicated-Entries-from-MySQL-Table.html</link>
            <category>Database</category>
    
    <comments>http://webconsults.eu/archives/61-Remove-Duplicated-Entries-from-MySQL-Table.html#comments</comments>
    <wfw:comment>http://webconsults.eu/wfwcomment.php?cid=61</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://webconsults.eu/rss.php?version=2.0&amp;type=comments&amp;cid=61</wfw:commentRss>
    

    <author>nospam@example.com (John Behrens)</author>
    <content:encoded>
    This is a short tutorial how to remove duplicated entries from MySQL DB Table

First idea was just to do a simple DELETE statement on a SubSelect 
&lt;div class=&quot;sql&quot; style=&quot;text-align: left&quot;&gt;&lt;br /&gt;&lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;DELETE&lt;/span&gt; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;FROM&lt;/span&gt; user &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;WHERE&lt;/span&gt; id &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;IN&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;SELECT&lt;/span&gt; u2.id &lt;br /&gt;&lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;FROM&lt;/span&gt; user &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;AS&lt;/span&gt; u1&lt;br /&gt;&lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;JOIN&lt;/span&gt; user &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;AS&lt;/span&gt; u2 &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;ON&lt;/span&gt; u1.name=u2.name&lt;br /&gt;&lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;WHERE&lt;/span&gt;&amp;#160; u1.id &amp;lt; u2.id&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;#160;&lt;/div&gt;
Unfortunatelly this gives back an error message.
&lt;em&gt;#1093 - You can&#039;t specify target table &#039;ex_user&#039; for update in FROM clause &lt;/em&gt;
MySQL Documentation says about that
&lt;blockquote&gt;
Error 1093 (ER_UPDATE_TABLE_USED)
SQLSTATE = HY000
Message = &quot;You can&#039;t specify target table &#039;x&#039;
for update in FROM clause&quot;

This error occurs in cases such as the following:

UPDATE t1 SET column2 = (SELECT MAX(column1) FROM t1);

You can use a subquery for assignment within an UPDATE statement because subqueries are legal in UPDATE and DELETE statements as well as in SELECT statements. However, you cannot use the same table (in this case, table t1) for both the subquery&#039;s FROM clause and the update target. 
&lt;/blockquote&gt;
I was searching for a solution without using temporary table but didn´t find one yet. 

&lt;div class=&quot;sql&quot; style=&quot;text-align: left&quot;&gt;&lt;br /&gt;&lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;TEMPORARY&lt;/span&gt; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;TABLE&lt;/span&gt; duplicated_temp&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;id INT&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; TYPE=HEAP;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;INTO&lt;/span&gt; duplicated_temp &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;SELECT&lt;/span&gt; u2.id&amp;#160; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;FROM&lt;/span&gt; ex_user &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;AS&lt;/span&gt; u1 &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;JOIN&lt;/span&gt; user &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;AS&lt;/span&gt; u2 &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;ON&lt;/span&gt; u1.name=u2.name &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;WHERE&lt;/span&gt; u1.id &amp;lt; u2.id;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;DELETE&lt;/span&gt; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;FROM&lt;/span&gt; user &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;WHERE&lt;/span&gt; id &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;IN&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;SELECT&lt;/span&gt; id &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;FROM&lt;/span&gt; duplicated_temp &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;WHERE&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;DROP&lt;/span&gt; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;TABLE&lt;/span&gt; duplicated_temp;&lt;br /&gt;&amp;#160;&lt;/div&gt;
Well thats it. 
 
    </content:encoded>

    <pubDate>Mon, 08 Feb 2010 14:13:38 +0100</pubDate>
    <guid isPermaLink="false">http://webconsults.eu/archives/61-guid.html</guid>
    
</item>

</channel>
</rss>
