<?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; MySQL</title>
	<atom:link href="http://ersinacar.com/tag/mysql/feed" rel="self" type="application/rss+xml" />
	<link>http://ersinacar.com</link>
	<description>PHP and Web Technologies Freak</description>
	<lastBuildDate>Fri, 06 Jan 2012 14:19:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Using Fulltext Indexes in MySQL &#8211; Part 2, Boolean searches</title>
		<link>http://ersinacar.com/using-fulltext-indexes-in-mysql-part-2-boolean-searches_189.html</link>
		<comments>http://ersinacar.com/using-fulltext-indexes-in-mysql-part-2-boolean-searches_189.html#comments</comments>
		<pubDate>Fri, 26 Mar 2010 18:53:06 +0000</pubDate>
		<dc:creator>Ersin Acar</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[fulltext]]></category>

		<guid isPermaLink="false">http://ersinacar.com/?p=189</guid>
		<description><![CDATA[Part 1 of this article looked at the fulltext index, and how to search on it using an ordinary MATCH() AGAINST(). Even more powerful, (although only available on the newer MySQL version 4), is the ability to do a boolean search. Part 2 of this article examines the possibilities. You&#8217;ll use the same table you [...]]]></description>
			<content:encoded><![CDATA[<p>Part 1 of this article looked at the fulltext index, and how to search on it using an ordinary MATCH() AGAINST(). Even more powerful, (although only available on the newer MySQL version 4), is the ability to do a boolean search. Part 2 of this article examines the possibilities.</p>
<p>You&#8217;ll use the same table you used in Part 1. The full list of records is:</p>
<pre>mysql&gt; SELECT * FROM fulltext_sample;
+---------------------------+
| copy                      |
+---------------------------+
| It appears good from here |
| The here and the past     |
| Why are we here           |
| An all-out alert          |
| All you need is love      |
| A good alert              |
+---------------------------+</pre>
<p>To perform a boolean search, the IN BOOLEAN MODE modifier is used. The following query demonstrates the new syntax.</p>
<p><span id="more-189"></span></p>
<pre>mysql&gt; SELECT * FROM fulltext_sample
WHERE MATCH (copy) AGAINST ('love' IN BOOLEAN MODE);
+----------------------+
| copy                 |
+----------------------+
| All you need is love |
+----------------------+</pre>
<p>An important difference between ordinary MATCH() AGAINST() queries and IN BOOLEAN MODE searches is that the latter does not apply the 50% limit (so the word can appear in more than half the rows).</p>
<p>There are a number of special operators you&#8217;ll need to know in order to make the most of the boolean search. If no symbol is specified, the appearance of this word causes the relevance of the row to be higher, similar to an ordinary MATCH() AGAINST().</p>
<h3>Boolean Search Operators</h3>
<table border="0">
<tbody>
<tr>
<td>+</td>
<td>The word is mandatory in all rows returned.</td>
</tr>
<tr>
<td>-</td>
<td>The word cannot appear in any row returned.</td>
</tr>
<tr>
<td>&lt;</td>
<td>The word that follows has a lower relevance than other words, although rows containing it will still match</td>
</tr>
<tr>
<td>&gt;</td>
<td>The word that follows has a higher relevance than other words.</td>
</tr>
<tr>
<td>()</td>
<td>Used to group words into subexpressions.</td>
</tr>
<tr>
<td>~</td>
<td>The word following contributes negatively to the relevance of the row (which is different to the &#8216;-&#8217; operator, which specifically excludes the word, or the &#8216;&lt;&#8217; operator, which still causes the word to contribute positively to the relevance of the row.</td>
</tr>
<tr>
<td>*</td>
<td>The wildcard, indicating zero or more characters. It can only appear at the end of a word.</td>
</tr>
<tr>
<td>&#8220;</td>
<td>Anything enclosed in the double quotes is taken as a whole (so you can match phrases, for example).</td>
</tr>
</tbody>
</table>
<h3>Examples</h3>
<p>Let&#8217;s see some of this action. The first example returns all rows containing the word &#8216;here&#8217;, but not the word &#8216;past&#8217;.</p>
<pre>mysql&gt; SELECT copy FROM fulltext_sample WHERE MATCH(copy)
AGAINST ('+here -past' IN BOOLEAN MODE);
+---------------------------+
| copy                      |
+---------------------------+
| It appears good from here |
| Why are we here           |
+---------------------------+</pre>
<p>Compare the difference between the next two examples:</p>
<pre>mysql&gt; SELECT copy FROM fulltext_sample WHERE MATCH(copy)
AGAINST ('here past' IN BOOLEAN MODE);
+---------------------------+
| copy                      |
+---------------------------+
| It appears good from here |
| The here and the past     |
| Why are we here           |
+---------------------------+

mysql&gt; SELECT copy FROM fulltext_sample WHERE MATCH(copy)
AGAINST ('here +past'IN BOOLEAN MODE);
+-----------------------+
| copy                  |
+-----------------------+
| The here and the past |
+-----------------------+</pre>
<p>The first works in a similar way to an ordinary MATCH() AGAINST(), albeit without the 50% threshold. The word &#8216;past&#8217; is mandatory in the second search (by default a word is optional), so the other two records are not returned.</p>
<p>The next example demonstrates a common mistake.</p>
<pre>mysql&gt; SELECT copy FROM fulltext_sample
WHERE MATCH(copy) AGAINST ('+are here' IN BOOLEAN MODE);
+---------------------------+
| copy                      |
+---------------------------+
| It appears good from here |
| The here and the past     |
| Why are we here           |
+---------------------------+</pre>
<p>The results may appear surprising compared the previous example, but since &#8216;are&#8217; contains three or less letters, it is excluded for purposes of the search and is not mandatory.</p>
<p>The next two examples demonstrate a powerful enhancement for searching purposes:</p>
<pre>mysql&gt; SELECT copy FROM fulltext_sample
WHERE MATCH(copy) AGAINST ('aler' IN BOOLEAN MODE);
Empty set (0.01 sec)

mysql&gt; SELECT copy FROM fulltext_sample
WHERE MATCH(copy) AGAINST ('aler*' IN BOOLEAN MODE);
+------------------+
| copy             |
+------------------+
| An all-out alert |
| A good alert     |
+------------------+</pre>
<pre></pre>
<pre>
By default only whole words are matched, unless the '*' operator is used.

The '&lt;' and '&gt;' symbols are less commonly used, but they allow a great degree of control for relevance. In the following examples, we return the relevance indicator to demonstrate the difference between the queries.
<pre>mysql&gt; SELECT copy, MATCH(copy)
AGAINST ('appears good alert' IN BOOLEAN MODE)
AS m FROM fulltext_sample WHERE MATCH(copy)
AGAINST ('appears good alert' IN BOOLEAN MODE);
+---------------------------+------+
| copy                      | m    |
+---------------------------+------+
| It appears good from here |    2 |
| An all-out alert          |    1 |
| A good alert              |    2 |
+---------------------------+------+

mysql&gt; SELECT copy, MATCH(copy)
AGAINST ('appears &lt;good alert' IN BOOLEAN MODE)
AS m FROM fulltext_sample WHERE MATCH(copy)
AGAINST ('appears &lt;good alert' IN BOOLEAN MODE);
+---------------------------+------------------+
| copy                      | m                |
+---------------------------+------------------+
| It appears good from here |  1.6666667461395 |
| An all-out alert          |                1 |
| A good alert              |  1.6666667461395 |
+---------------------------+------------------+</pre>
<p>The '&lt;' operator decreases the relevance of the word 'good', in this case by approximately 0.33.</p>
<pre>mysql&gt; SELECT copy, MATCH(copy)
AGAINST ('appears good &gt;alert' IN BOOLEAN MODE)
AS m FROM fulltext_sample WHERE MATCH(copy)
AGAINST ('appears good &gt;alert' IN BOOLEAN MODE);
+---------------------------+------+
| copy                      | m    |
+---------------------------+------+
| It appears good from here |    2 |
| An all-out alert          |  1.5 |
| A good alert              |  2.5 |
+---------------------------+------+</pre>
<p>The '&gt;' operator increases the relevance of the word 'alert', in this case by 0.5.</p>
<p>The parentheses group words into a subexpression. In the following example, the '+' symbol applies to the group of words, so that at least one of 'appears' or 'past' must appear.</p>
<pre>mysql&gt; SELECT copy, MATCH(copy)
AGAINST ('+(appears past)' IN BOOLEAN MODE)
AS m FROM fulltext_sample WHERE MATCH(copy)
AGAINST ('(appears past)' IN BOOLEAN MODE);
+---------------------------+------+
| copy                      | m    |
+---------------------------+------+
| It appears good from here |    1 |
| The here and the past     |    1 |
+---------------------------+------+</pre>
<p>You can also apply the operators to words in the subexpression, as follows:</p>
<pre>mysql&gt; SELECT copy, MATCH(copy)
AGAINST ('+(&gt;appears &lt;past)' IN BOOLEAN MODE)
AS m FROM fulltext_sample WHERE MATCH(copy)
AGAINST ('+(&gt;appears &lt;past)' IN BOOLEAN MODE);
+---------------------------+------------------+
| copy                      | m                |
+---------------------------+------------------+
| It appears good from here |              1.5 |
| The here and the past     | 0.66666668653488 |
+---------------------------+------------------+</pre>
<p>The '~' operator contributes negatively to the relevance, but does not bar the word from appearing, as the '-' operator does. It also does not do the same as the '&lt;' operator, which is to make a reduced, yet still positive, difference to the relevance. These subtle difference allows for powerfully tuned searches, but can cause confusion. Look at the difference between the following. First, the word 'here' is compulsory, and the word 'past' optional:</p>
<pre>mysql&gt; SELECT copy, MATCH(copy)
AGAINST ('+here past' IN BOOLEAN MODE)
AS m FROM fulltext_sample WHERE MATCH(copy)
AGAINST ('+here past' IN BOOLEAN MODE);
+---------------------------+-----------------+
| copy                      | m               |
+---------------------------+-----------------+
| It appears good from here |               1 |
| The here and the past     | 1.3333333730698 |
| Why are we here           |               1 |
+---------------------------+-----------------+</pre>
<p>Next, a reduced relevance for the word 'past'. The same results are returned, but the record with both words ('The here and the past') has less weighting, though still more than the other records.</p>
<pre>mysql&gt; SELECT copy, MATCH(copy)
AGAINST ('+here &lt;past' IN BOOLEAN MODE)
AS m FROM fulltext_sample WHERE MATCH(copy)
AGAINST ('+here &lt;past' IN BOOLEAN MODE);
+---------------------------+-----------------+
| copy                      | m               |
+---------------------------+-----------------+
| It appears good from here |               1 |
| The here and the past     | 1.2222222089767 |
| Why are we here           |               1 |
+---------------------------+-----------------+</pre>
<p>Take note of the relevance in the next example:</p>
<pre>mysql&gt; SELECT copy, MATCH(copy)
AGAINST ('+here ~past' IN BOOLEAN MODE)
AS m FROM fulltext_sample WHERE MATCH(copy)
AGAINST ('+here ~past' IN BOOLEAN MODE);
+---------------------------+------------------+
| copy                      | m                |
+---------------------------+------------------+
| It appears good from here |                1 |
| The here and the past     | 0.83333331346512 |
| Why are we here           |                1 |
+---------------------------+------------------+</pre>
<p>The important difference is that the relevance is negatively affected, and is now lower than the other two records. This means if you asked for only '~past', you'd get nothing back, as the relevance would be lower than 0 (and therefore not returned)</p>
<pre>mysql&gt; SELECT copy, MATCH(copy)
AGAINST ('~past' IN BOOLEAN MODE)
AS m FROM fulltext_sample WHERE MATCH(copy)
AGAINST ('~past' IN BOOLEAN MODE);
Empty set (0.00 sec)</pre>
<p>Finally, '-' simply removes the second record from the result set, as we've seen before.</p>
<pre>mysql&gt; SELECT copy, MATCH(copy)
AGAINST ('+here -past' IN BOOLEAN MODE)
AS m FROM fulltext_sample WHERE MATCH(copy)
AGAINST ('+here -past' IN BOOLEAN MODE);
+---------------------------+------+
| copy                      | m    |
+---------------------------+------+
| It appears good from here |    1 |
| Why are we here           |    1 |
+---------------------------+------+</pre>
<p>The final operator, the double quotes ("), is useful to group phrases together. So, if you were interested in returning 'good from here' but none of those words if they did not appear in the phrase, you'd use:</p>
<pre>mysql&gt; SELECT copy, MATCH(copy)
AGAINST ('"good from here"' IN BOOLEAN MODE)
AS m FROM fulltext_sample WHERE MATCH(copy)
AGAINST ('"good from here"' IN BOOLEAN MODE);
+---------------------------+------+
| copy                      | m    |
+---------------------------+------+
| It appears good from here |    1 |
+---------------------------+------+</pre>
<p>For many applications, boolean searches are one of MySQL 4's most useful features. With careful use, you can save yourself substantial of development time, and add many useful enhancements. Good luck!</pre>
]]></content:encoded>
			<wfw:commentRss>http://ersinacar.com/using-fulltext-indexes-in-mysql-part-2-boolean-searches_189.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mysql Dump and Restore commands</title>
		<link>http://ersinacar.com/mysql-dump-and-restore-commands_125.html</link>
		<comments>http://ersinacar.com/mysql-dump-and-restore-commands_125.html#comments</comments>
		<pubDate>Thu, 21 May 2009 17:50:55 +0000</pubDate>
		<dc:creator>Ersin Acar</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[dump]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[restore]]></category>

		<guid isPermaLink="false">http://ersinacar.com/?p=125</guid>
		<description><![CDATA[if you want to backup and restore your big MySQL databases you need better solution than phpmyadmin So here we go; To dump all databases to a file: $ mysqldump -u dbuser -pdbpass --all-databases &#62; all-databases.sql To dump a single database to a file: $ mysqldump -u dbuser -pdbpass somedb &#62; somedb.sql Dump InnoDB in [...]]]></description>
			<content:encoded><![CDATA[<p>if you want to backup and restore your big MySQL databases you need better solution than phpmyadmin</p>
<p>So here we go;</p>
<p>To dump  all databases to a file:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ mysqldump <span style="color: #660033;">-u</span> dbuser <span style="color: #660033;">-pdbpass</span> <span style="color: #660033;">--all-databases</span> <span style="color: #000000; font-weight: bold;">&gt;</span> all-databases.sql</pre></div></div>

<p>To dump  a single database to a file:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ mysqldump <span style="color: #660033;">-u</span> dbuser <span style="color: #660033;">-pdbpass</span> somedb <span style="color: #000000; font-weight: bold;">&gt;</span> somedb.sql</pre></div></div>

<p>Dump InnoDB in a single transaction:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ mysqldump <span style="color: #660033;">-u</span> dbuser <span style="color: #660033;">-pdbpass</span> <span style="color: #660033;">--single-transaction</span> somedb <span style="color: #000000; font-weight: bold;">&gt;</span> somedb.sql</pre></div></div>

<p>To restore:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ mysql <span style="color: #660033;">-u</span> dbuser <span style="color: #660033;">-pdbpass</span> somedb <span style="color: #000000; font-weight: bold;">&lt;</span> somedb.sql</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://ersinacar.com/mysql-dump-and-restore-commands_125.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Change/Fix Wrong MySQL Encoding</title>
		<link>http://ersinacar.com/changefix-wrong-mysql-encoding_89.html</link>
		<comments>http://ersinacar.com/changefix-wrong-mysql-encoding_89.html#comments</comments>
		<pubDate>Fri, 24 Apr 2009 09:37:00 +0000</pubDate>
		<dc:creator>Ersin Acar</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[charset]]></category>
		<category><![CDATA[encoding]]></category>
		<category><![CDATA[wrong]]></category>

		<guid isPermaLink="false">http://ersinacar.com/?p=89</guid>
		<description><![CDATA[Sometimes, especially when moving data from one server to another, you might find that you have your MySQL database incorrectly encoded. Problem with this first show itself if you have the database and encoded in one charset set to display your website in another. If this is the case then you will find strange characters [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes, especially when moving data from one server to another, you might find that you have your MySQL database incorrectly encoded. Problem with this first show itself if you have the database and encoded in one charset set to display your website in another. If this is the case then you will find strange characters appearing in your text, especially when using punctuation marks. If you are Unwilling or unable to change the character encoding on the site then you need to change how the data is encoded in the database.</p>
<p>The most common sort of thing you might want to do is change from iso-8859-1 (or windows-1252) to UTF-8. This can be done in one of two ways.</p>
<p>The first way is to simply alter the table so that the column contains a different charset.</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">ALTER</span> <span style="color: #990099; font-weight: bold;">TABLE</span> <span style="color: #990099; font-weight: bold;">table</span> <span style="color: #990099; font-weight: bold;">MODIFY</span> col1 <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">50</span><span style="color: #FF00FF;">&#41;</span> CHARACTER <span style="color: #990099; font-weight: bold;">SET</span> <span style="color: #008000;">'utf8'</span><span style="color: #000033;">;</span></pre></div></div>

<p>However, if your database has already been set up and your data has already been inserted in the wrong format then you can also update the data in the column using the CONVERT command. The following snippet turns our latin1 data into uncoded binary data and then into utf8.</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">UPDATE</span> <span style="color: #990099; font-weight: bold;">table</span> <span style="color: #990099; font-weight: bold;">SET</span> col1<span style="color: #CC0099;">=</span><span style="color: #990099; font-weight: bold;">CONVERT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">CONVERT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">CONVERT</span><span style="color: #FF00FF;">&#40;</span>col1 <span style="color: #990099; font-weight: bold;">USING</span> <span style="color: #008000;">'latin1'</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">USING</span> <span style="color: #990099; font-weight: bold;">BINARY</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">USING</span> <span style="color: #008000;">'utf8'</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></div></div>

<p>You should also make sure that the connection to the database is done through a specific character set. This is done by using the<br />
<span id="more-89"></span></p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SET</span> NAMES command <span style="color: #CC0099; font-weight: bold;">and</span> the <span style="color: #990099; font-weight: bold;">SET</span> CHARACTER <span style="color: #990099; font-weight: bold;">SET</span>.
&nbsp;
<span style="color: #990099; font-weight: bold;">SET</span> NAMES <span style="color: #008000;">'charset<span style="color: #008080; font-weight: bold;">_</span>name'</span>
<span style="color: #990099; font-weight: bold;">SET</span> CHARACTER <span style="color: #990099; font-weight: bold;">SET</span> <span style="color: #008000;">'charset<span style="color: #008080; font-weight: bold;">_</span>name'</span><span style="color: #000033;">;</span></pre></div></div>

<p>These two commands basically set some values in your MySQL database, for more information on what is set look at the Connection Character Sets and Collations page on the MySQL website. This ensures that the data we get back from the database is also in the correct charset.</p>
<p>For a full list of the different character sets available in MySQL just run the command:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SHOW</span> CHARACTER <span style="color: #990099; font-weight: bold;">SET</span><span style="color: #000033;">;</span></pre></div></div>

<p>This will display a table with the columns Charset, Description, Default collation and Maxlen. Each charset is associated with a collation. A collation is a set of rules for comparing characters in a charset, so it is important that you get this right if you want the database to work. The full list of collations can be viewed using the following command:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SHOW</span> <span style="color: #000099;">COLLATION</span><span style="color: #000033;">;</span></pre></div></div>

<p>You can even use a LIKE statement to refine the collation data into the information you are looking for.</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SHOW</span> <span style="color: #000099;">COLLATION</span> <span style="color: #990099; font-weight: bold;">WHERE</span> <span style="color: #FF9900; font-weight: bold;">Charset</span> <span style="color: #CC0099; font-weight: bold;">LIKE</span> <span style="color: #008000;">'<span style="color: #008080; font-weight: bold;">%</span>utf<span style="color: #008080; font-weight: bold;">%</span>'</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://ersinacar.com/changefix-wrong-mysql-encoding_89.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<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>Ersin Acar</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. [...]]]></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> <span style="color: #993333; font-weight: bold;">VALUE</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">COUNT</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">VALUE</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">COUNT</span>
<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> <span style="color: #993333; font-weight: bold;">VALUE</span>
<span style="color: #993333; font-weight: bold;">HAVING</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">COUNT</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">VALUE</span><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> <span style="color: #993333; font-weight: bold;">COUNT</span> <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> <span style="color: #993333; font-weight: bold;">VALUE</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">COUNT</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">VALUE</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">COUNT</span>
<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> <span style="color: #993333; font-weight: bold;">VALUE</span>
<span style="color: #993333; font-weight: bold;">HAVING</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">COUNT</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">VALUE</span><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> <span style="color: #993333; font-weight: bold;">COUNT</span> <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><span style="color: #993333; font-weight: bold;">NUMBER</span> <span style="color: #66cc66;">=</span> good_rows<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">NUMBER</span>
<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>
		<item>
		<title>Fix Wrong Character Encoding In MySQL</title>
		<link>http://ersinacar.com/fix-wrong-character-encoding-in-mysql_61.html</link>
		<comments>http://ersinacar.com/fix-wrong-character-encoding-in-mysql_61.html#comments</comments>
		<pubDate>Wed, 08 Apr 2009 22:31:04 +0000</pubDate>
		<dc:creator>Ersin Acar</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[charset]]></category>
		<category><![CDATA[collation]]></category>
		<category><![CDATA[encoding]]></category>

		<guid isPermaLink="false">http://ersinacar.com/?p=61</guid>
		<description><![CDATA[Sometimes, especially when moving data from one server to another, you might find that you have encoded your MySQL database incorrectly. This problem with first show itself if you have the database encoded in one charset and your website set to display in another. If this is the case then you will find strange characters [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes, especially when moving data from one server to another, you might find that you have encoded your MySQL database incorrectly. This problem with first show itself if you have the database encoded in one charset and your website set to display in another. If this is the case then you will find strange characters appearing in your text, especially when using punctuation marks. If you are unable or unwilling to change the character encoding on the site then you need to change how the data is encoded in the database.</p>
<p>The most common sort of thing you might want to do is change from iso-8859-1 (or windows-1252) to UTF-8. This can be done in one of two ways.<br />
<span id="more-61"></span><br />
The first way is to simply alter the table so that the column contains a different charset.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">MODIFY</span> col1 <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">CHARACTER</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #ff0000;">'utf8'</span>;</pre></div></div>

<p>However, if your database has already been set up and your data has already been inserted in the wrong format then you can also update the data in the column using the CONVERT command. The following snippet turns our latin1 data into uncoded binary data and then into utf8.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">SET</span> col1<span style="color: #66cc66;">=</span><span style="color: #993333; font-weight: bold;">CONVERT</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">CONVERT</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">CONVERT</span><span style="color: #66cc66;">&#40;</span>col1 <span style="color: #993333; font-weight: bold;">USING</span> <span style="color: #ff0000;">'latin1'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">USING</span> <span style="color: #993333; font-weight: bold;">BINARY</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">USING</span> <span style="color: #ff0000;">'utf8'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>You should also make sure that the connection to the database is done through a specific character set. This is done by using the SET NAMES command and the SET CHARACTER SET.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SET</span> NAMES <span style="color: #ff0000;">'charset_name'</span>
<span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #993333; font-weight: bold;">CHARACTER</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #ff0000;">'charset_name'</span>;</pre></div></div>

<p>These two commands basically set some values in your MySQL database, for more information on what is set look at the <a title="Connection Character Sets and Collations" href="http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html">Connection Character Sets and Collations</a> page on the MySQL website. This ensures that the data we get back from the database is also in the correct charset.</p>
<p>For a full list of the different character sets available in MySQL just run the command:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SHOW</span> <span style="color: #993333; font-weight: bold;">CHARACTER</span> <span style="color: #993333; font-weight: bold;">SET</span>;</pre></div></div>

<p>This will display a table with the columns Charset, Description, Default collation and Maxlen. Each charset is associated with a collation. A collation is a set of rules for comparing characters in a charset, so it is important that you get this right if you want the database to work. The full list of collations can be viewed using the following command:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SHOW</span> COLLATION;</pre></div></div>

<p>You can even use a LIKE statement to refine the collation data into the information you are looking for.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SHOW</span> COLLATION <span style="color: #993333; font-weight: bold;">WHERE</span> Charset <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'%utf%'</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://ersinacar.com/fix-wrong-character-encoding-in-mysql_61.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

