<?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>ria-coder.com &#187; conditionals</title>
	<atom:link href="http://ria-coder.com/blog/tag/conditionals/feed" rel="self" type="application/rss+xml" />
	<link>http://ria-coder.com/blog</link>
	<description></description>
	<lastBuildDate>Wed, 21 Apr 2010 19:35:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Using Conditional Statements with MySQL</title>
		<link>http://ria-coder.com/blog/using-conditional-statements-with-mysql</link>
		<comments>http://ria-coder.com/blog/using-conditional-statements-with-mysql#comments</comments>
		<pubDate>Thu, 14 May 2009 19:36:59 +0000</pubDate>
		<dc:creator>Danny Kopping</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[conditionals]]></category>

		<guid isPermaLink="false">http://ria-coder.com/blog/?p=276</guid>
		<description><![CDATA[While working on a project, i got lazy (as i always do...); I didn't feel like fetching an array of rows from MySQL and filtering the data in PHP according to a condition...]]></description>
			<content:encoded><![CDATA[<p>While working on a project, i got lazy (as i always do&#8230;); I didn&#8217;t feel like fetching an array of rows from MySQL and filtering the data in PHP according to a condition&#8230; Too much of a ballache to be honest. I thought: &#8216;<em>F</em><em>uck it&#8230; Let&#8217;s see if one can use conditional statements in MySQL</em>&#8216;<em>.</em></p>
<p>After doing a little research, i came across this page (<a title="http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html" href="http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html" target="_blank">http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html</a>). After scouring through the normally indecipherable MySQL reference manual, i managed to get the hang of it&#8230;</p>
<p>Although the scenarios where the application of this functionality are few and far between, i felt it&#8217;d still be a cool idea to share this (seemingly) arbitrary tit-bit.</p>
<p>Consider the following scenario&#8230;<br />
You have a table of users. In this table, you keep the following information about each user:</p>
<blockquote>
<ul>
<li>name</li>
<li>surname</li>
<li>gender</li>
</ul>
</blockquote>
<p>Now, depending on the gender of your user, you would like to return their details with a certain prefix, namely <strong>Mr </strong>for a male or <strong>Ms</strong> for a female. Here&#8217;s the database structure, and two rows of sample data:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #ff0000;">`users`</span>
<span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`name`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><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;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`surname`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><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;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`gender`</span> enum<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'M'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'F'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>MyISAM <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>latin1 ROW_FORMAT<span style="color: #66cc66;">=</span>DYNAMIC;
&nbsp;
<span style="color: #808080; font-style: italic;">--</span>
<span style="color: #808080; font-style: italic;">-- Sample data</span>
<span style="color: #808080; font-style: italic;">--</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`users`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`name`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`surname`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`gender`</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Joe'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Shmo'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'M'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Jane'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Shmane'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'F'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>&#8230;and here&#8217;s the SQL query</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*,</span> CONCAT<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">IF</span><span style="color: #66cc66;">&#40;</span>gender <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;M&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;Mr &quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;Ms &quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> name<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot; &quot;</span><span style="color: #66cc66;">,</span> surname<span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">AS</span> fullName
<span style="color: #993333; font-weight: bold;">FROM</span> users</pre></div></div>

<p>If that looks a little too indecipherable/complicated, here&#8217;s the previous query in a more simplified (albeit less efficient) way:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*,</span> <span style="color: #993333; font-weight: bold;">IF</span><span style="color: #66cc66;">&#40;</span>gender <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;M&quot;</span><span style="color: #66cc66;">,</span> CONCAT<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Mr &quot;</span><span style="color: #66cc66;">,</span> name<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot; &quot;</span><span style="color: #66cc66;">,</span> surname<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
			CONCAT<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Ms &quot;</span><span style="color: #66cc66;">,</span> name<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot; &quot;</span><span style="color: #66cc66;">,</span> surname<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">AS</span> fullName
<span style="color: #993333; font-weight: bold;">FROM</span> users</pre></div></div>

<p><a rel="lightbox" href="http://ria-coder.com/temp/wp-content/uploads/2009/05/conditionals.png" class="lightbox-enabled" title="Using Conditionals in MySQL"><img class="size-medium wp-image-281 alignleft" title="conditionals" src="http://ria-coder.com/blog/wp-content/uploads/2009/05/conditionals-300x235.png" alt="conditionals" width="300" height="235"  hspace="10"/></a>Essentially the IF() statement is structured like this:</p>
<p>IF(condition, true code, false code)</p>
<p>However, you can place an IF statement anywhere in a query (from  what i can tell thus far), it gets executed in place and is replaced by  either the <strong>true</strong> code or the <strong>false</strong> code.</p>
<p>Another (much simplified) example:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">IF</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;TRUE!&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;FALSE!&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>&#8230;and that&#8217;s conditionals with MySQL!</p>
]]></content:encoded>
			<wfw:commentRss>http://ria-coder.com/blog/using-conditional-statements-with-mysql/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

