<?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; Scripts</title>
	<atom:link href="http://www.slickdev.com/category/scripts/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: 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>
		<item>
		<title>Friendster Script</title>
		<link>http://www.slickdev.com/2007/08/14/friendster-script/</link>
		<comments>http://www.slickdev.com/2007/08/14/friendster-script/#comments</comments>
		<pubDate>Mon, 13 Aug 2007 16:56:51 +0000</pubDate>
		<dc:creator>Chaoz</dc:creator>
				<category><![CDATA[Scripts]]></category>

		<guid isPermaLink="false">http://www.slickdev.com/2007/08/14/friendster-script/</guid>
		<description><![CDATA[Well, seems like a problem crept into a friend&#8217;s Friendster account. Everything in her page disappeared, or so it seems. The problem lay inside the shoutbox. A rouge script has inserted a small CSS script inside and made every &#8220;Div&#8221; element invisible. How? I dont know. I made this script for her to run under [...]]]></description>
			<content:encoded><![CDATA[<p>Well, seems like a problem crept into a friend&#8217;s Friendster account. Everything in her page disappeared, or so it seems. The problem lay inside the shoutbox. A rouge script has inserted a small CSS script inside and made every &#8220;Div&#8221; element invisible. How? I dont know. I made this script for her to run under greasemonkey. This will rewrite what&#8217;s inside the shoutbox and let her rewrite whats inside back to normal.</p>
<p>The greasemonkey script can be found under the script page or <a href="http://www.slickdev.com/scripts/friendster-re-write-shoutbox-content-script/">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.slickdev.com/2007/08/14/friendster-script/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
