<?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>Ashevile Web Design and Computer Networking :: Applied Solutions Group &#187; find in set</title>
	<atom:link href="http://www.appliedtns.com/blog/tag/find-in-set/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.appliedtns.com/blog</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Wed, 28 Apr 2010 02:39:50 +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>MYSQL search in comma deliminated field.</title>
		<link>http://www.appliedtns.com/blog/2008/11/mysql-search-in-comma-deliminated-field/</link>
		<comments>http://www.appliedtns.com/blog/2008/11/mysql-search-in-comma-deliminated-field/#comments</comments>
		<pubDate>Wed, 26 Nov 2008 19:33:53 +0000</pubDate>
		<dc:creator>Tony G.</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[comma delimited values]]></category>
		<category><![CDATA[find in set]]></category>
		<category><![CDATA[in command]]></category>
		<category><![CDATA[MYSQL]]></category>

		<guid isPermaLink="false">http://asgblog.com/?p=38</guid>
		<description><![CDATA[It seems it is not possible to combine the MySQL IN command with a field name, even when that field contains comma delimited values. MySQL may match the first value, but not any of those following the comma.

SELECT name FROM pet_owners WHERE 'FIDO' IN &#40;pet_owners.pets&#41;

The above code doesn&#8217;t work, I suspect because

... IN &#40;pet_owners.pets&#41; ...

is [...]]]></description>
			<content:encoded><![CDATA[<p>It seems it is not possible to combine the MySQL IN command with a field name, even when that field contains comma delimited values. MySQL may match the first value, but not any of those following the comma.</p>

<div class="wp_syntax"><div class="code"><pre class="mysql mysql" style="font-family:monospace;"><span style="color:#9FBFFE; font-weight:bold;">SELECT</span> name <span style="color:#9FBFFE; font-weight:bold;">FROM</span> pet_owners <span style="color:#9FBFFE; font-weight:bold;">WHERE</span> <span style="color:#A8FF60;">'FIDO'</span> <span style="color:#9FBFFE; font-weight:bold;">IN</span> <span style="color:#AA66FF; font-weight:bold;">&#40;</span>pet_owners.pets<span style="color:#AA66FF; font-weight:bold;">&#41;</span></pre></div></div>

<p>The above code doesn&#8217;t work, I suspect because</p>

<div class="wp_syntax"><div class="code"><pre class="mysql mysql" style="font-family:monospace;">... <span style="color:#9FBFFE; font-weight:bold;">IN</span> <span style="color:#AA66FF; font-weight:bold;">&#40;</span>pet_owners.pets<span style="color:#AA66FF; font-weight:bold;">&#41;</span> ...</pre></div></div>

<p>is treated not as</p>

<div class="wp_syntax"><div class="code"><pre class="mysql mysql" style="font-family:monospace;">... <span style="color:#9FBFFE; font-weight:bold;">IN</span> <span style="color:#AA66FF; font-weight:bold;">&#40;</span><span style="color:#A8FF60;">'FLUFFY'</span> , <span style="color:#A8FF60;">'FIDO'</span> , <span style="color:#A8FF60;">'SPOT'</span><span style="color:#AA66FF; font-weight:bold;">&#41;</span> ...</pre></div></div>

<p>but as</p>

<div class="wp_syntax"><div class="code"><pre class="mysql mysql" style="font-family:monospace;">... <span style="color:#9FBFFE; font-weight:bold;">IN</span> <span style="color:#AA66FF; font-weight:bold;">&#40;</span><span style="color:#A8FF60;">'FLUFFY,FIDO,SPOT'</span><span style="color:#AA66FF; font-weight:bold;">&#41;</span> ...</pre></div></div>

<p><strong>THE SOLUTION</strong></p>
<p>One method is to use Regular Expresisons:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql mysql" style="font-family:monospace;"><span style="color:#9FBFFE; font-weight:bold;">SELECT</span> name <span style="color:#9FBFFE; font-weight:bold;">FROM</span> pet_owners <span style="color:#9FBFFE; font-weight:bold;">WHERE</span> pet_owners.pets <span style="">REGEXP</span> <span style="color:#A8FF60;">'[[:&lt;:]]FIDO[[:&gt;:]]'</span></pre></div></div>

<p>Another way would be to use the <a href="http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set">FIND IN SET</a> command:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql mysql" style="font-family:monospace;"><span style="color:#9FBFFE; font-weight:bold;">SELECT</span> name <span style="color:#9FBFFE; font-weight:bold;">FROM</span> pet_owners <span style="color:#9FBFFE; font-weight:bold;">WHERE</span> <span style="color:#9FBFFE; font-weight:bold;">FIND</span> <span style="color:#9FBFFE; font-weight:bold;">IN</span> <span style="color:#9FBFFE; font-weight:bold;">SET</span> <span style="color:#AA66FF; font-weight:bold;">&#40;</span><span style="color:#A8FF60;">'FIDO'</span>, pet_owners.pets<span style="color:#AA66FF; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&gt;</span> 0</pre></div></div>

<p>The pet_owners.pets field does not have to be set to datatype SET as long as it has comman separated values.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.appliedtns.com/blog/2008/11/mysql-search-in-comma-deliminated-field/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
