<?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>marpeta &#187; mysql</title>
	<atom:link href="http://marpeta.lt/category/technology/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://marpeta.lt</link>
	<description>Searching for the answer to Life, the Universe, and Everything</description>
	<lastBuildDate>Mon, 23 Aug 2010 20:24:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
<cloud domain='marpeta.lt' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>MySQL trick with set of values using IN()</title>
		<link>http://marpeta.lt/2009/03/mysql-trick-with-set-of-values-using-in/</link>
		<comments>http://marpeta.lt/2009/03/mysql-trick-with-set-of-values-using-in/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 19:43:18 +0000</pubDate>
		<dc:creator>kraupu</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[tech]]></category>

		<guid isPermaLink="false">http://marpeta.lt/?p=799</guid>
		<description><![CDATA[Atsiprašau, mielieji lietuviškieji skaitytojai, bet šitas įrašas yra skirtas specifinei vartotojų grupei, todėl bus parašytas anglų kalba. Ačiū ir iki :) I just want to mention really simple issue with set of values in MySQL5 because I managed to get trapped by this simple issue for couple of hours trying to understand why the result [...]]]></description>
			<content:encoded><![CDATA[<p><span style="font-style: italic;">Atsiprašau, mielieji lietuviškieji skaitytojai, bet šitas įrašas yra skirtas specifinei vartotojų grupei, todėl bus parašytas anglų kalba. Ačiū ir iki :)</span></p>
<p><span id="more-799"></span></p>
<hr />I just want to mention really simple issue with set of values in MySQL5 because I managed to get trapped by this simple issue for couple of hours trying to understand why the result in couple of lines of code is so weird. When I managed to figure it out I sent this simple trick to couple of colleagues and they couldn&#8217;t come with the solution in a matter of minutes so I thought that it would be interesting to mention it and let google index it for others.</p>
<p>To make this short, simple and interesting let&#8217;s try playing the game.</p>
<p>If there was <em>&#8220;What Geek Wants to be a Millionaire&#8221;</em> then this could go for the last question. You know the pressure and all could take couple of minutes to figure the answer out :)</p>
<h3>Questions</h3>
<p>So, here it goes. We have three simple MySQLv5 queries executed on default MySQL configuration. You have to give the result for these three simple queries.</p>
<p><strong>1</strong>:</p>
<pre>SELECT ('mysql' IN (0)) AS '@result';</pre>
<p><strong>2</strong>:</p>
<pre>SELECT ('mysql' IN (1)) AS 'result';</pre>
<p><strong>3</strong>:</p>
<pre>SELECT ('mysql' IN ('sql')) AS result;</pre>
<p>If you know the answer so you could find the trick very quickly and you&#8217;re the winner of &#8220;Who Wants to be a Millionaire&#8221; :)</p>
<h3>Answers</h3>
<p>The answers are quite simple as queries. Here it goes:</p>
<p><strong>1</strong>. The problem with the first query is that <strong><em>&#8216;mysql&#8217;</em></strong> is of string type and <strong><em>0</em></strong> is numeric and there has to be type casting. So string <strong><em>&#8216;mysql&#8217;</em></strong> is casted into numeric <strong><em>0</em></strong> and now we have query where we want to find <strong><em>0</em></strong> in set of only one value and that value is <strong><em>0</em></strong>:</p>
<pre>SELECT (0 IN (0))</pre>
<p>So the result will be <strong><em>1</em></strong>:</p>
<pre>mysql&gt; SELECT ('mysql' IN (0)) AS '@result';
+---------+
| @result |
+---------+
|       1 |
+---------+
1 row in set, 1 warning (0.01 sec)</pre>
<pre>mysql&gt; SHOW WARNINGS;
+---------+------+-------------------------------------------+
| Level   | Code | Message                                   |
+---------+------+-------------------------------------------+
| Warning | 1292 | Truncated incorrect DOUBLE value: 'mysql' |
+---------+------+-------------------------------------------+
1 row in set (0.00 sec)</pre>
<p><strong>2</strong>. As I explained in the first example the query could be translated into:</p>
<pre>SELECT (0 IN (1))</pre>
<p>So the result will be <em><strong>0</strong></em>.</p>
<p><strong>3</strong>. This query is just for those newbies that don&#8217;t know MySQL&#8217;s <a target="_blank" href="http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in" >IN()</a> :) The answer unquestionably is <em><strong>0</strong></em>.</p>
<p>So the first query was with the trick and I hope that there would be couple of people that will save ton of time trying to debug some similar query to first example.</p>
]]></content:encoded>
			<wfw:commentRss>http://marpeta.lt/2009/03/mysql-trick-with-set-of-values-using-in/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
