<?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>Bright Cherry &#187; MySQL</title>
	<atom:link href="http://www.brightcherry.co.uk/scribbles/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.brightcherry.co.uk/scribbles</link>
	<description></description>
	<lastBuildDate>Fri, 27 Aug 2010 18:00:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>MySQL &#8211; INSERT SELECT Example</title>
		<link>http://www.brightcherry.co.uk/scribbles/2010/02/14/mysql-insert-select-example/</link>
		<comments>http://www.brightcherry.co.uk/scribbles/2010/02/14/mysql-insert-select-example/#comments</comments>
		<pubDate>Sun, 14 Feb 2010 11:01:47 +0000</pubDate>
		<dc:creator>Maruf</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.brightcherry.co.uk/scribbles/?p=196</guid>
		<description><![CDATA[I&#8217;m quickly going to demonstrate how to INSERT rows from one table into another table. I&#8217;d just like to clarify, this isn&#8217;t the same as completely copying a table, because with a INSERT SELECT query you can use the WHERE clause to INSERT rows with a particular condition (e.g. WHERE name = &#8216;Bill&#8217;). Whereas, copying [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m quickly going to demonstrate how to INSERT rows from one table into another table. I&#8217;d just like to clarify, this isn&#8217;t the same as completely copying a table, because with a INSERT SELECT query you can use the WHERE clause to INSERT rows with a particular condition (e.g. WHERE name = &#8216;Bill&#8217;). Whereas, copying an entire table will copy every single record.</p>
<h4>Table Structure</h4>
<p>This table is called <b class="pink">Postcode_tbl</b>:<br />
<img src="http://www.brightcherry.co.uk/images/mysql_duplicate/1.jpg" title="Postcode Table Structure" alt="Postcode Table Structure" /></p>
<h4>INSERT SELECT Query Example</h4>
<p>Let&#8217;s assume we have a table with the exact same structure as <b class="pink">Postcode_tbl</b>, but it&#8217;s called <b class="pink">Postcode_tbl_2</b>, and we want to copy all the rows from <b class="pink">Postcode_tbl</b> into <b class="pink">Postcode_tbl_2</b> where the first 2 characters of the postcode is &#8220;CM&#8221;.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup></sup></span><span class="left"><a rel="nofollow" href="javascript:;" onclick="javascript:showCodeTxt('p196code2'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1962"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p196code2"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span>
Postcode_tbl_2
<span style="color: #FF00FF;">&#40;</span>
postcode<span style="color: #FF00FF;">,</span>
longitude<span style="color: #FF00FF;">,</span>
latitude
<span style="color: #FF00FF;">&#41;</span>
<span style="color: #990099; font-weight: bold;">SELECT</span>
postcode<span style="color: #FF00FF;">,</span>
longitude<span style="color: #FF00FF;">,</span>
latitude
<span style="color: #990099; font-weight: bold;">FROM</span>
Postcode_tbl
<span style="color: #990099; font-weight: bold;">WHERE</span>
postcode <span style="color: #CC0099; font-weight: bold;">LIKE</span> <span style="color: #008000;">'CM%'</span></pre></td></tr></table></div>

<p>Now, the table structure DOESN&#8217;T need to be the same in order to use a INSERT SELECT query. I just used the same structure for ease. If the table structure differs, or the field names don&#8217;t match, just make sure you specify the correct field names and order of fields in your query.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.brightcherry.co.uk/scribbles/2010/02/14/mysql-insert-select-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL- Remove Duplicate Rows</title>
		<link>http://www.brightcherry.co.uk/scribbles/2009/11/22/mysql-remove-duplicate-rows/</link>
		<comments>http://www.brightcherry.co.uk/scribbles/2009/11/22/mysql-remove-duplicate-rows/#comments</comments>
		<pubDate>Sun, 22 Nov 2009 12:25:50 +0000</pubDate>
		<dc:creator>Maruf</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.brightcherry.co.uk/scribbles/?p=189</guid>
		<description><![CDATA[I&#8217;ve been working with meduim sized tables (2.1million rows) in MySQL lately. One particular table had a lot of duplicate rows, which I needed to filter out. I&#8217;m quickly going to demonstrate how I did it. I&#8217;m sure there are many ways of doing this, but this method proved to be the easiest for me. [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working with meduim sized tables (2.1million rows) in MySQL lately. One particular table had a lot of duplicate rows, which I needed to filter out. I&#8217;m quickly going to demonstrate how I did it. I&#8217;m sure there are many ways of doing this, but this method proved to be the easiest for me.</p>
<h4>Example 1: Removing rows with a specific duplicate field:</h4>
<p>As you can see, the field &#8220;postcode&#8221; has duplicated rows.<br />
<img src="http://www.brightcherry.co.uk/images/mysql_duplicate/1.jpg" title="Duplicate MySQL field" alt="Duplicate MySQL field" /></p>
<h4>STEP 1: Copy table structure</h4>
<p>I used the following code to duplicate the table structure of `postcodes` (if you use PhpMyAdmin, you can use the shortcuts):</p>

<div class="wp_codebox_msgheader"><span class="right"><sup></sup></span><span class="left"><a rel="nofollow" href="javascript:;" onclick="javascript:showCodeTxt('p189code6'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1896"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p189code6"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> <span style="color: #008000;">`DB_NAME`</span>.<span style="color: #008000;">`postcodes2`</span> <span style="color: #FF00FF;">&#40;</span>
<span style="color: #008000;">`postcodenospace`</span> <span style="color: #FF9900; font-weight: bold;">varchar</span><span style="color: #FF00FF;">&#40;</span> <span style="color: #008080;">10</span> <span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #FF00FF;">,</span>
<span style="color: #008000;">`longitude`</span> <span style="color: #FF9900; font-weight: bold;">varchar</span><span style="color: #FF00FF;">&#40;</span> <span style="color: #008080;">15</span> <span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #FF00FF;">,</span>
<span style="color: #008000;">`latitude`</span> <span style="color: #FF9900; font-weight: bold;">varchar</span><span style="color: #FF00FF;">&#40;</span> <span style="color: #008080;">15</span> <span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span>
<span style="color: #FF00FF;">&#41;</span></pre></td></tr></table></div>

<h4>STEP 2: run query:</h4>

<div class="wp_codebox_msgheader"><span class="right"><sup></sup></span><span class="left"><a rel="nofollow" href="javascript:;" onclick="javascript:showCodeTxt('p189code7'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1897"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p189code7"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span>
postcodes2
<span style="color: #FF00FF;">&#40;</span>
postcode<span style="color: #FF00FF;">,</span>
longitude<span style="color: #FF00FF;">,</span>
latitude
<span style="color: #FF00FF;">&#41;</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> postcode<span style="color: #FF00FF;">,</span>longitude<span style="color: #FF00FF;">,</span>latitude
<span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #008000;">`postcodes`</span> 
<span style="color: #990099; font-weight: bold;">GROUP</span> <span style="color: #990099; font-weight: bold;">BY</span> postcode</pre></td></tr></table></div>

<p>That&#8217;s it. All the rows with a unique postcode will get inserted into the table &#8220;postcodes2&#8243;.</p>
<h4>The result</h4>
<p><img src="http://www.brightcherry.co.uk/images/mysql_duplicate/2.jpg" title="Duplicate MySQL field result" alt="Duplicate MySQL field result" /></p>
<p>The key is in the SELECT query, where the postcode field is grouped together. That effectively puts all the duplicate postcodes together,  generating a result of rows with unique postcodes.</p>
<h4>Example 2: Removing duplicate rows:</h4>
<h4>STEP 1: Copy table structure</h4>
<p>Use the code above to copy the table structure.</p>
<p>This example is slightly different, look at the data:</p>
<p><img src="http://www.brightcherry.co.uk/images/mysql_duplicate/3.jpg" title="Duplicate MySQL rows" alt="Duplicate MySQL rows" /></p>
<p>The previous example simply filtered the data by duplicate postcodes. As you can see, the highlighted row has different longitude and latitude values. So if you want to filter the data by unique rows, you need the following query:</p>
<h4>STEP 2: Run the query</h4>

<div class="wp_codebox_msgheader"><span class="right"><sup></sup></span><span class="left"><a rel="nofollow" href="javascript:;" onclick="javascript:showCodeTxt('p189code8'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1898"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p189code8"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span>
postcodes2
<span style="color: #FF00FF;">&#40;</span>
postcode<span style="color: #FF00FF;">,</span>
longitude<span style="color: #FF00FF;">,</span>
latitude
<span style="color: #FF00FF;">&#41;</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> postcode<span style="color: #FF00FF;">,</span>longitude<span style="color: #FF00FF;">,</span>latitude
<span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #008000;">`postcodes`</span> 
<span style="color: #990099; font-weight: bold;">GROUP</span> <span style="color: #990099; font-weight: bold;">BY</span> <span style="color: #000099; font-weight: bold;">concat</span><span style="color: #FF00FF;">&#40;</span>postcode<span style="color: #FF00FF;">,</span>longitude<span style="color: #FF00FF;">,</span>latitude<span style="color: #FF00FF;">&#41;</span></pre></td></tr></table></div>

<h4>The result</h4>
<p><img src="http://www.brightcherry.co.uk/images/mysql_duplicate/4.jpg" title="Duplicate MySQL rows result" alt="Duplicate MySQL rows result" /></p>
<p>Again, the key is the SELECT query. This time the query groups all the fields together (using the CONCAT function), to return rows with completely unique values.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.brightcherry.co.uk/scribbles/2009/11/22/mysql-remove-duplicate-rows/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>MySQL- Counting The Total Occurances Of A Regular Expression In A Table Column</title>
		<link>http://www.brightcherry.co.uk/scribbles/2009/07/16/mysql-how-to-count-a-regular-expression-in-a-table-column/</link>
		<comments>http://www.brightcherry.co.uk/scribbles/2009/07/16/mysql-how-to-count-a-regular-expression-in-a-table-column/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 22:59:58 +0000</pubDate>
		<dc:creator>Maruf</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.brightcherry.co.uk/scribbles/?p=150</guid>
		<description><![CDATA[Ok, so here&#8217;s the scenerio- you have a table like this in your DB: The question is: How do you count how many images there are in the images column for the entire table? From what I&#8217;m aware, you can&#8217;t do it with a standard MySQL function. So the solution? You have to create your [...]]]></description>
			<content:encoded><![CDATA[<p>Ok, so here&#8217;s the scenerio- you have a table like this in your DB:</p>
<p><img src="http://www.brightcherry.co.uk/images/blogimages/mysql-count-reg-ex.jpg" title="How To Count A Regular Expression In A Column" alt="How To Count A Regular Expression In A Column" /></p>
<p><strong>The question is: </strong></p>
<h4>How do you count how many images there are in the images column for the entire table?</h4>
<p>From what I&#8217;m aware, you can&#8217;t do it with a standard MySQL function. So the solution? You have to create your own function. The function needs to count how many times &#8220;.jpg&#8221; is present in the &#8220;image&#8221; column.</p>
<h4>Solution: Step 1- Create the Function</h4>

<div class="wp_codebox_msgheader"><span class="right"><sup></sup></span><span class="left"><a rel="nofollow" href="javascript:;" onclick="javascript:showCodeTxt('p150code11'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p15011"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p150code11"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> substrCount<span style="color: #66cc66;">&#40;</span>x varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> delim varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">12</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> returns int
<span style="color: #993333; font-weight: bold;">return</span> <span style="color: #66cc66;">&#40;</span>length<span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">-</span>length<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">,</span>delim<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">/</span>length<span style="color: #66cc66;">&#40;</span>delim<span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>Run the above query to create your MySQL function.</p>
<h4>Solution: Step 2- Run the query with your new function</h4>

<div class="wp_codebox_msgheader"><span class="right"><sup></sup></span><span class="left"><a rel="nofollow" href="javascript:;" onclick="javascript:showCodeTxt('p150code12'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p15012"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p150code12"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> SUM<span style="color: #66cc66;">&#40;</span>substrCount<span style="color: #66cc66;">&#40;</span>images<span style="color: #66cc66;">,</span><span style="color: #ff0000;">'.jpg'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">from</span> <span style="color: #993333; font-weight: bold;">TABLE</span></pre></td></tr></table></div>

<p>Run that query and you&#8217;ll get the total times &#8220;.jpg&#8221; occurs in the &#8216;images&#8217; column, consequently giving you a total count of how many images there are.</p>
<h4>Notes</h4>
<p>The MySQL function you created (substrCount) will remain stored in your DB until it is manually dropped, so you won&#8217;t need to create it again- you can call it whenever you want.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.brightcherry.co.uk/scribbles/2009/07/16/mysql-how-to-count-a-regular-expression-in-a-table-column/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
