<?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>SlickDev &#187; MySQL</title>
	<atom:link href="http://www.slickdev.com/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.slickdev.com</link>
	<description>Freelance Web and Software Developer</description>
	<lastBuildDate>Sat, 15 May 2010 02:27:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>MySQL Slave Failed to Open the Relay Log</title>
		<link>http://www.slickdev.com/2008/10/20/mysql-slave-failed-to-open-the-relay-log/</link>
		<comments>http://www.slickdev.com/2008/10/20/mysql-slave-failed-to-open-the-relay-log/#comments</comments>
		<pubDate>Mon, 20 Oct 2008 14:53:32 +0000</pubDate>
		<dc:creator>Chaoz</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[server]]></category>

		<guid isPermaLink="false">http://www.slickdev.com/?p=68</guid>
		<description><![CDATA[This problem is a little tricky, there are possible fixes that MySQL website has stated. Sad to say, the one&#8217;s I read in the forum and site didn&#8217;t fix my problem. What I encountered was that the relay-bin from my MySQL slave server has already been &#8216;rotated&#8217;, meaning deleted from the folder. This happens when [...]]]></description>
			<content:encoded><![CDATA[<p>This problem is a little tricky, there are possible fixes that MySQL website has stated. Sad to say, the one&#8217;s I read in the forum and site didn&#8217;t fix my problem. What I encountered was that the relay-bin from my MySQL  slave server has already been &#8216;rotated&#8217;, meaning <em>deleted</em> from the folder. This happens when the slave has been disconnected from the master for quite a long time already and has not replicated anything. A simple way to fix this is to flush the logs, but make sure the slave is stopped before using this command&#8230;</p>
<p><code>FLUSH LOGS;</code></p>
<p>Bring in a fresh copy of the database from the master-server and update the slave-server database. THIS IS IMPORTANT! Since if you don&#8217;t update the slave database, you will not have the data from the time you were disconnected until you reset the relay logs. So UPDATE YOUR SLAVE WITH THE LATEST DATABASE FROM THE MASTER!</p>
<p>Now when the logs are flushed,all the relay-bin logs will be deleted when the slave is started again. Usually, this fixes the problem, but when you start the slave and the failed relay log error is still there, now you have to do some more desperate measures&#8230; reset the slave. This is what I had to do to fully restore my MySQL slave server. Reseting the slaves restores all the settings to default&#8230; password, username, relay-log, port, table to replicate, etc&#8230; So better to have a copy of your settings first before actually do a slave reset. When your ready to rest the slave, do the command&#8230;</p>
<p><code>RESET SLAVE;</code></p>
<p>after which you should restore all your setting with a command something like&#8230;</p>
<p><code>CHANGE MASTER TO MASTER_HOST=.....</code></p>
<p>now start your server with&#8230;</p>
<p><code>SLAVE START;</code></p>
<p>check your slave server with&#8230;</p>
<p><code>SHOW SLAVE STATUS\G</code></p>
<p>look for &#8230;</p>
<p><code>Slave_IO_Running: Yes<br />
Slave_SQL_Running: Yes</code></p>
<p>both should be <strong>YES</strong>, if not, check your syslog if there are other errors encountered. I&#8217;ll leave this until here since this is what I encountered and I was able to fix it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.slickdev.com/2008/10/20/mysql-slave-failed-to-open-the-relay-log/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL: Query Real Values from Delimiter-Separated-String-IDs</title>
		<link>http://www.slickdev.com/2008/09/15/mysql-query-real-values-from-delimiter-separated-string-ids/</link>
		<comments>http://www.slickdev.com/2008/09/15/mysql-query-real-values-from-delimiter-separated-string-ids/#comments</comments>
		<pubDate>Mon, 15 Sep 2008 14:15:37 +0000</pubDate>
		<dc:creator>Chaoz</dc:creator>
				<category><![CDATA[Code Library]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Scripts]]></category>

		<guid isPermaLink="false">http://www.slickdev.com/?p=54</guid>
		<description><![CDATA[I had a problem in using a string-comma-separated-value returned from a query in an &#8220;IN&#8221; statement. I used the IDs from one table, concat them into a comma separated value and insert them into another table. Baaaad idea&#8230; Now when I query that value, I can&#8217;t use it directly into an &#8220;IN&#8221; statement to retrieve [...]]]></description>
			<content:encoded><![CDATA[<p>I had a problem in using a string-comma-separated-value returned from a query in an &#8220;IN&#8221; statement. I used the IDs from one table, <em>concat</em> them into a comma separated value and insert them into another table. Baaaad idea&#8230; Now when I query that value, I can&#8217;t use it directly into an &#8220;IN&#8221; statement to retrieve their real values since its a string being returned from the query, the &#8220;IN&#8221; statement will not compare all the values inside as a set, but it will compare it as a string. </p>
<p>e.g.<br />
<strong>SELECT value FROM my_table WHERE my_id IN (&#8217;1, 2, 3&#8242;)</strong> is NOT equivalent to <strong>SELECT value FROM my_table WHERE my_id IN (&#8217;1&#8242;, &#8217;2&#8242;, &#8217;3&#8242;)</strong></p>
<p>So, if you have a table containing values 1 to 3, the first query will return only <strong>1</strong> while the second query will return all values; <strong>1, 2 and 3</strong>.</p>
<p>I Googled around and found out that MySQL does not have a native equivalent of PHP&#8217;s <em>explode()</em> function. Crap&#8230; I had to do it the hard war and create a MySQL stored function to &#8216;explode&#8217; the values from its delimiter, query the right value from the other table using the exploded IDs, concat them back together and return them as a string.</p>
<p>Below is the function I was able to patch together from different codes I found in the MySQL forum. I added comments below as to make things clearer. I named the function <strong>splitAndTranslate</strong> since that&#8217;s what I was really trying to implement. You can make up your own modifications and function name.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;">DELIMITER <span style="color: #66cc66;">//</span>
<span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #ff0000;">`splitAndTranslate`</span> <span style="color: #66cc66;">//</span>
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> splitAndTranslate<span style="color: #66cc66;">&#40;</span>str TEXT<span style="color: #66cc66;">,</span> delim VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">124</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
RETURNS TEXT
DETERMINISTIC
BEGIN
	DECLARE i INT <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #cc66cc;">0</span>;	<span style="color: #808080; font-style: italic;">-- total number of delimiters</span>
	DECLARE ctr INT <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #cc66cc;">0</span>;	<span style="color: #808080; font-style: italic;">-- counter for the loop</span>
	DECLARE str_len INT;		<span style="color: #808080; font-style: italic;">-- string length,self explanatory</span>
	DECLARE out_str text <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">''</span>;	<span style="color: #808080; font-style: italic;">-- return string holder</span>
	DECLARE temp_str text <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">''</span>;	<span style="color: #808080; font-style: italic;">-- temporary string holder</span>
  	DECLARE temp_val VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">''</span>;	<span style="color: #808080; font-style: italic;">-- temporary string holder for query</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">-- get length</span>
	<span style="color: #993333; font-weight: bold;">SET</span> str_len<span style="color: #66cc66;">=</span>LENGTH<span style="color: #66cc66;">&#40;</span>str<span style="color: #66cc66;">&#41;</span>;	
&nbsp;
	<span style="color: #993333; font-weight: bold;">SET</span> i <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span>LENGTH<span style="color: #66cc66;">&#40;</span>str<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>str<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> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span>;	
		<span style="color: #808080; font-style: italic;">-- get total number delimeters and add 1</span>
		<span style="color: #808080; font-style: italic;">-- add 1 since total separated values are 1 more than the number of delimiters</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">-- start of while loop</span>
	WHILE<span style="color: #66cc66;">&#40;</span>ctr<span style="color: #66cc66;">&lt;</span>i<span style="color: #66cc66;">&#41;</span> DO
		<span style="color: #808080; font-style: italic;">-- add 1 to the counter, which will also be used to get the value of the string</span>
		<span style="color: #993333; font-weight: bold;">SET</span> ctr<span style="color: #66cc66;">=</span>ctr<span style="color: #66cc66;">+</span><span style="color: #cc66cc;">1</span>; 
&nbsp;
		<span style="color: #808080; font-style: italic;">-- get value separated by delimiter using ctr as the index</span>
		<span style="color: #993333; font-weight: bold;">SET</span> temp_str <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>SUBSTRING<span style="color: #66cc66;">&#40;</span>SUBSTRING_INDEX<span style="color: #66cc66;">&#40;</span>str<span style="color: #66cc66;">,</span> delim<span style="color: #66cc66;">,</span> ctr<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> LENGTH<span style="color: #66cc66;">&#40;</span>SUBSTRING_INDEX<span style="color: #66cc66;">&#40;</span>str<span style="color: #66cc66;">,</span> delim<span style="color: #66cc66;">,</span>ctr <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</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: #66cc66;">,</span> delim<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">-- query real value and insert into temporary value holder, temp_str contains the exploded ID    		</span>
		<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">&lt;</span>real_value_column<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">INTO</span> temp_val <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">&lt;</span>my_table<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #66cc66;">&lt;</span>table_id<span style="color: #66cc66;">&gt;=</span>temp_str;
&nbsp;
		<span style="color: #808080; font-style: italic;">-- concat real value into output string separated by delimiter</span>
    		<span style="color: #993333; font-weight: bold;">SET</span> out_str<span style="color: #66cc66;">=</span>CONCAT<span style="color: #66cc66;">&#40;</span>out_str<span style="color: #66cc66;">,</span> temp_val<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">','</span><span style="color: #66cc66;">&#41;</span>;
	END WHILE;
	<span style="color: #808080; font-style: italic;">-- end of while loop</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">-- trim delimiter from end of string</span>
	<span style="color: #993333; font-weight: bold;">SET</span> out_str<span style="color: #66cc66;">=</span>TRIM<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">TRAILING</span> delim <span style="color: #993333; font-weight: bold;">FROM</span> out_str<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #993333; font-weight: bold;">RETURN</span><span style="color: #66cc66;">&#40;</span>out_str<span style="color: #66cc66;">&#41;</span>;	<span style="color: #808080; font-style: italic;">-- return </span>
&nbsp;
END<span style="color: #66cc66;">//</span></pre></td></tr></table></div>

<p>After creating the stored function, you can now use it normally like any MySQL function inside a query. So what I now do with the new function is&#8230;</p>
<p><strong>SELECT splitAndTranslate( g.comma_separated_ids ) real_values FROM my_group_table g;</strong></p>
<p>Thanks to Chris Stubben in the <a href="http://dev.mysql.com/doc/refman/5.0/en/string-functions.html">MySQL Forum</a>, I used and modified his code to fit my need.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.slickdev.com/2008/09/15/mysql-query-real-values-from-delimiter-separated-string-ids/feed/</wfw:commentRss>
		<slash:comments>36</slash:comments>
		</item>
	</channel>
</rss>
