<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ersin Acar &#187; dublicate</title>
	<atom:link href="http://ersinacar.com/tag/dublicate/feed" rel="self" type="application/rss+xml" />
	<link>http://ersinacar.com</link>
	<description>PHP and Web Technologies Freak</description>
	<lastBuildDate>Fri, 14 May 2010 11:57:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Deleting dublicated rows in MySQL</title>
		<link>http://ersinacar.com/deleting-dublicated-rows-in-mysql_65.html</link>
		<comments>http://ersinacar.com/deleting-dublicated-rows-in-mysql_65.html#comments</comments>
		<pubDate>Thu, 09 Apr 2009 08:44:09 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[delete]]></category>
		<category><![CDATA[dublicate]]></category>
		<category><![CDATA[rows]]></category>

		<guid isPermaLink="false">http://ersinacar.com/?p=65</guid>
		<description><![CDATA[To find duplicate values you need to use the MySQL COUNT() function and then pick out all of the counts that are greater than one.

SELECT value,COUNT&#40;value&#41; AS Count
FROM test
GROUP BY value
HAVING &#40;COUNT&#40;value&#41; &#38;gt; 1&#41;
ORDER BY Count DESC;

Conversely you can also select the rows that only have a single entry.

SELECT value,COUNT&#40;value&#41; AS Count
FROM test
GROUP BY value
HAVING [...]]]></description>
			<content:encoded><![CDATA[<p>To find duplicate values you need to use the MySQL COUNT() function and then pick out all of the counts that are greater than one.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> value<span style="color: #66cc66;">,</span>COUNT<span style="color: #66cc66;">&#40;</span>value<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> Count
<span style="color: #993333; font-weight: bold;">FROM</span> test
<span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> value
<span style="color: #993333; font-weight: bold;">HAVING</span> <span style="color: #66cc66;">&#40;</span>COUNT<span style="color: #66cc66;">&#40;</span>value<span style="color: #66cc66;">&#41;</span> &amp;gt; <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> Count <span style="color: #993333; font-weight: bold;">DESC</span>;</pre></div></div>

<p>Conversely you can also select the rows that only have a single entry.<span id="more-65"></span></p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> value<span style="color: #66cc66;">,</span>COUNT<span style="color: #66cc66;">&#40;</span>value<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> Count
<span style="color: #993333; font-weight: bold;">FROM</span> test
<span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> value
<span style="color: #993333; font-weight: bold;">HAVING</span> <span style="color: #66cc66;">&#40;</span>COUNT<span style="color: #66cc66;">&#40;</span>value<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span>; <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> Count <span style="color: #993333; font-weight: bold;">DESC</span>;</pre></div></div>

<p>However, it is very nice to pick out the duplicate entries in a table, but you might still need to do something with them. Here is a query to delete any duplicate rows from a table. It does a simple self join and deletes the row value with the lowest ID.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">DELETE</span> bad_rows<span style="color: #66cc66;">.*</span>
<span style="color: #993333; font-weight: bold;">FROM</span> tests <span style="color: #993333; font-weight: bold;">AS</span> good_rows
<span style="color: #993333; font-weight: bold;">INNER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> tests <span style="color: #993333; font-weight: bold;">AS</span> bad_rows <span style="color: #993333; font-weight: bold;">ON</span> bad_rows<span style="color: #66cc66;">.</span>number <span style="color: #66cc66;">=</span> good_rows<span style="color: #66cc66;">.</span>number
<span style="color: #993333; font-weight: bold;">AND</span> bad_rows<span style="color: #66cc66;">.</span>id &amp;gt; good_rows<span style="color: #66cc66;">.</span>id;</pre></div></div>

<p>More information on this deletion query and other methods of deleting duplicates can be found at <a href="http://www.xaprb.com/blog/2006/10/11/how-to-delete-duplicate-rows-with-sql/">Xaprb.com</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ersinacar.com/deleting-dublicated-rows-in-mysql_65.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
