<?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>PSEiBlog &#187; boolean</title>
	<atom:link href="http://www.pseiko.nl/blog/tag/boolean/feed" rel="self" type="application/rss+xml" />
	<link>http://www.pseiko.nl/blog</link>
	<description>Lalalala… uh… huh???</description>
	<lastBuildDate>Wed, 04 Aug 2010 13:58:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>MySQL FULLTEXT search Checklist</title>
		<link>http://www.pseiko.nl/blog/126/mysql-fulltext-search-checklist</link>
		<comments>http://www.pseiko.nl/blog/126/mysql-fulltext-search-checklist#comments</comments>
		<pubDate>Thu, 14 May 2009 14:39:39 +0000</pubDate>
		<dc:creator>Remco</dc:creator>
				<category><![CDATA[Computer & Technology Related]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[against]]></category>
		<category><![CDATA[boolean]]></category>
		<category><![CDATA[fulltext]]></category>
		<category><![CDATA[innodb]]></category>
		<category><![CDATA[like]]></category>
		<category><![CDATA[match]]></category>
		<category><![CDATA[mode]]></category>
		<category><![CDATA[myisam]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[select]]></category>

		<guid isPermaLink="false">http://www.pseiko.nl/blog/?p=126</guid>
		<description><![CDATA[In an effort to help a colleague with FULLTEXT search troubles today, I tried to find out everything that could go wrong with setting up this search method on a table. My short research resulted in this checklist. Failure to comply with these checks will result in catastrophic failure 1) Make sure the table you&#8217;re [...]]]></description>
			<content:encoded><![CDATA[<p>In an effort to help a colleague with FULLTEXT search troubles today, I tried to find out everything that could go wrong with setting up this search method on a table.<br />
My short research resulted in this checklist. Failure to comply with these checks will result in catastrophic failure <img src='http://www.pseiko.nl/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /><br />
<span id="more-126"></span><br />
1) Make sure the table you&#8217;re trying to FULLTEXT on has MyISAM type! Currently, FULLTEXT searches do *NOT* work on InnoDB types. If you are bound to using InnoDB for various reasons, you could create a duplicate table in MyISAM to perform your searches on.<br />
2) Ensure that your tables have Indexes with the FULLTEXT type for all columns you&#8217;re trying to perform FULLTEXT searches on. If you don&#8217;t have such an index, you can easily add it by using:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td 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> xxx <span style="color: #990099; font-weight: bold;">ADD</span> <span style="color: #FF9900; font-weight: bold;">FULLTEXT</span><span style="color: #FF00FF;">&#40;</span>yyy<span style="color: #000033;">,</span>zzz<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>Where xxx is your tablename, and yyy + zzz are the columns you want to search. Do this for earch individual column or set of columns you want to search.<br />
3) If you changed anything in your setup, you might want to rebuild the Indexes. This is done by issuing:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="mysql" style="font-family:monospace;">REPAIR <span style="color: #990099; font-weight: bold;">TABLE</span> xxx<span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>4) If you are building a search function for a product catalog, it&#8217;s likely that you have to match against short words. e.g. a product that has the name &#8217;16C&#8217;. FULLTEXT defaults to using 4 characters for the shortest possible word, so your &#8217;16C&#8217; is not a valid word, and will be omitted in search results. You can change this by editing your my.cnf (likely to reside in <code>/etc/mysql/my.cnf</code> on linux based systems), and adding the <code>ft_min_word_len</code> directive to the appropriate sections:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #FF00FF;">&#91;</span>mysqld<span style="color: #FF00FF;">&#93;</span>
ft_min_word_len <span style="color: #CC0099;">=</span> <span style="color: #008080;">2</span>
&nbsp;
<span style="color: #FF00FF;">&#91;</span>myisamchk<span style="color: #FF00FF;">&#93;</span>
ft_min_word_len <span style="color: #CC0099;">=</span> <span style="color: #008080;">2</span></pre></td></tr></table></div>

<p>Do note that shortening the minimum word length significantly increases the load on your MySQL server, so this might be a VERY bad idea on large tables/busy systems. (You might as well go back to using <code>%LIKE%</code> searches <img src='http://www.pseiko.nl/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ).<br />
After you added the directives, restart MySQL (e.g. <code>/etc/init.d/mysql restart</code>), and rebuild your table indexes using the REPAIR command from check 3.<br />
5) BLOB is a commonly used fieldtype for fields that contain large bits of content. BLOB is a binary type though, and it disables the possibility to use FULLTEXT searches on fields that have the BLOB type.<br />
Solution is to convert the BLOB fields you want to search to the non-binary TEXT type. (Or any equivalent type of your choise). This can be done by issuing:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td 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> xxx <span style="color: #990099; font-weight: bold;">MODIFY</span> yyy <span style="color: #999900; font-weight: bold;">TEXT</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>Where xxx is the tablename and yyy the fieldname.<br />
6) Last but not least.. make sure you have a MySQL version running that supports FULLTEXT searching. Basic FULLTEXT has been around since 3.23.23, and if you want to use the more advanced IN BOOLEAN MODE directive in your AGAINST(), you need to have a whopping MySQL 4.1 or better available to you. (Just install MySQL5 and you&#8217;re in the green!;))</p>
<p>If you are GO on all these checks, and it still doesn&#8217;t work.. it might very well be your Query that&#8217;s messed up. Although the FULLTEXT basics are out of this blogposts scope , here&#8217;s a very basic FULLTEXT query that should work:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> xxx <span style="color: #990099; font-weight: bold;">WHERE</span> <span style="color: #990099; font-weight: bold;">MATCH</span><span style="color: #FF00FF;">&#40;</span>yyy<span style="color: #000033;">,</span>zzz<span style="color: #FF00FF;">&#41;</span> AGAINST <span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'+aaa -bbb'</span> <span style="color: #990099; font-weight: bold;">IN</span> <span style="color: #999900; font-weight: bold;">BOOLEAN</span> MODE<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>xxx = table, yyy + zzz are columns (make sure they&#8217;re indexed in a group!), aaa + bbb are Strings. aaa will be included, bbb excluded.</p>
<p>Good luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pseiko.nl/blog/126/mysql-fulltext-search-checklist/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Substituting boolean values within a MySQL SELECT query</title>
		<link>http://www.pseiko.nl/blog/65/substituting-boolean-values-within-a-mysql-select-query</link>
		<comments>http://www.pseiko.nl/blog/65/substituting-boolean-values-within-a-mysql-select-query#comments</comments>
		<pubDate>Mon, 27 Apr 2009 14:07:38 +0000</pubDate>
		<dc:creator>Remco</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[boolean]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[replace]]></category>
		<category><![CDATA[select]]></category>
		<category><![CDATA[update]]></category>

		<guid isPermaLink="false">http://www.pseiko.nl/blog/?p=65</guid>
		<description><![CDATA[Replacing 1/0 boolean values returned by a MySQL SELECT query can easily be substituted in the MySQL result set using the following syntax:    

SELECT REPLACE(REPLACE(boolColumn,'0','false'),'1','true') AS booleanValue FROM tableName;]]></description>
			<content:encoded><![CDATA[<p>Today I faced a quite interesting problem, that originated from pure lazyness.<br />
I&#8217;m developing a backend system for a quite complex database structure. Within this backend, an almost limitless amount of table views have to be created for the end user. Because I&#8217;m extremely lazy, and didn&#8217;t want to develop the html view code for each table view, I created a PHP html-table-generator-class, which takes a mysql_result_set as parameter, and outputs the html table in string format.</p>
<p>This method works great, unless for some cases, where a value in the query has to be substituted by a user readable value. A boolean is a good example of such a value.</p>
<p><span id="more-65"></span></p>
<p><strong>Booleans in MySQL<br />
</strong>Since MySQL doesn&#8217;t natively support boolean values, there are various methods of saving booleans to a database. Common practices include fields of the format CHAR(1), TINYINT(1) and ENUM(&#8216;Y&#8217;,'N&#8217;). Recent versions of MySQL also include the BIT fieldformat, which in fact boils down to using TINYINT(1), when it comes to boolean values.</p>
<p>I always use the TINYINT(1) fieldformat for my boolean columns. This works perfectly, since you can INSERT true/false values, which are automatically transformed to 1/0. When you SELECT a boolean column in PHP, this also works, since you can compare true/false to 1/0 within PHP. (Whether this loose type handling is preferable is a discussion for another time).</p>
<p>However.. when you SELECT a boolean column, and run it through a generic html table generator class such as mine, you&#8217;ll end up with a nice table showing 1&#8242;s and 0&#8242;s, which aren&#8217;t very userfriendly (Having in mind that most backend users aren&#8217;t quite as computer savvy as you are!). Replacing all 1&#8242;s and 0&#8242;s within the PHP code was not an option, since it would be a generic measure, and would also transform ID&#8217;s and Prices and such. So I had to come up with a solution to substitute the boolean values within the MySQL result by human readable values.</p>
<p><strong>Substituting boolean values within a MySQL SELECT query<br />
</strong>The solution is in fact, as they usually are, quite simple. MySQL supports the use of the <a href="http://dev.mysql.com/doc/refman/5.0/en/replace.html">REPLACE()</a> SQL command. Usually this command is used to UPDATE tables with replacement values, but it also works within a SELECT query to alter results. Having this in mind I started out replacing one value in the result:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #000099;">REPLACE</span><span style="color: #FF00FF;">&#40;</span>boolColumn<span style="color: #000033;">,</span><span style="color: #008000;">'1'</span><span style="color: #000033;">,</span><span style="color: #008000;">'true'</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">AS</span> booleanValue <span style="color: #990099; font-weight: bold;">FROM</span> tableName<span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>This works! There&#8217;s a slight catch though, since &#8216;false&#8217; values also have to be substituted. After some fiddling with IF and AND constructions, the solutions was ofcourse a lot easier:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #000099;">REPLACE</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">REPLACE</span><span style="color: #FF00FF;">&#40;</span>boolColumn<span style="color: #000033;">,</span><span style="color: #008000;">'0'</span><span style="color: #000033;">,</span><span style="color: #008000;">'false'</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span><span style="color: #008000;">'1'</span><span style="color: #000033;">,</span><span style="color: #008000;">'true'</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">AS</span> booleanValue <span style="color: #990099; font-weight: bold;">FROM</span> tableName<span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>And there you have it.. value substitution 101 in MySQL. This method also works for your other subsitution needs!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pseiko.nl/blog/65/substituting-boolean-values-within-a-mysql-select-query/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
