{"id":4198,"date":"2012-01-26T06:09:23","date_gmt":"2012-01-26T04:09:23","guid":{"rendered":"http:\/\/code.openark.org\/blog\/?p=4198"},"modified":"2012-01-26T06:09:23","modified_gmt":"2012-01-26T04:09:23","slug":"syntax-of-the-day-is-true-and-is-false","status":"publish","type":"post","link":"https:\/\/code.openark.org\/blog\/mysql\/syntax-of-the-day-is-true-and-is-false","title":{"rendered":"Syntax of the day: IS TRUE and IS FALSE"},"content":{"rendered":"<p>What makes for a <em>true<\/em> statement?<\/p>\n<p>We usually test statements using a WHERE clause:<\/p>\n<blockquote>\n<pre>SELECT * FROM world.City WHERE Population &gt; 1000000<\/pre>\n<\/blockquote>\n<p>The <strong>&#8220;Population &gt; 1000000&#8221;<\/strong> statement makes for a boolean expression. Using <strong>WHERE<\/strong> is just one way of evaluating it. One can also test with <a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/control-flow-functions.html#function_if\"><strong>IF()<\/strong><\/a>:<\/p>\n<blockquote>\n<pre>SET @val := 7;\r\nSELECT IF(@val &gt; 2, 'Yes', 'No')<\/pre>\n<\/blockquote>\n<h4>TRUE and FALSE<\/h4>\n<p>The two are keywords. They also map for the numerals <strong>1<\/strong> and <strong>0<\/strong>, as follows:<\/p>\n<blockquote>\n<pre>mysql&gt; SELECT TRUE, FALSE;\r\n+------+-------+\r\n| TRUE | FALSE |\r\n+------+-------+\r\n|\u00a0\u00a0\u00a0 1 |\u00a0\u00a0\u00a0\u00a0 0 |\r\n+------+-------+<\/pre>\n<\/blockquote>\n<p>Like in the <strong>C<\/strong> programming language, a nonzero value evaluates to a <em>true<\/em> value. A zero evaluates to <em>false<\/em>. A NULL evaluates to&#8230; well, NULL. But aside from 3-valued logic, what&#8217;s important in our case is that it is <em>not true<\/em>.<!--more--><\/p>\n<p>However, simple value comparison is incorrect:<\/p>\n<blockquote>\n<pre>mysql&gt; SELECT @val, @val &gt; 3, @val &gt; 3 = TRUE as result;\r\n+------+----------+--------+\r\n| @val | @val &gt; 3 | result |\r\n+------+----------+--------+\r\n|\u00a0\u00a0\u00a0 7 |\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1 |\u00a0\u00a0\u00a0\u00a0\u00a0 1 |\r\n+------+----------+--------+\r\n\r\nmysql&gt; SELECT @val, @val = TRUE as result;\r\n+------+--------+\r\n| @val | result |\r\n+------+--------+\r\n|\u00a0\u00a0\u00a0 7 |\u00a0\u00a0\u00a0\u00a0\u00a0 0 |\r\n+------+--------+<\/pre>\n<\/blockquote>\n<p>To test for the truth value of an expression, the correct syntax is by using <strong>IS TRUE<\/strong>:<\/p>\n<blockquote>\n<pre>SELECT @val, @val IS TRUE as result;\r\n+------+--------+\r\n| @val | result |\r\n+------+--------+\r\n|\u00a0\u00a0\u00a0 7 |\u00a0\u00a0\u00a0\u00a0\u00a0 1 |\r\n+------+--------+<\/pre>\n<\/blockquote>\n<p>Likewise, one may use <strong>IS FALSE<\/strong> to test for falsehood. However, if you wish to note <strong>NULL<\/strong> as a <em>false<\/em> value this does not work:<\/p>\n<blockquote>\n<pre>SELECT @empty, @empty IS TRUE, @empty IS FALSE;\r\n+--------+----------------+-----------------+\r\n| @empty | @empty IS TRUE | @empty IS FALSE |\r\n+--------+----------------+-----------------+\r\n| NULL\u00a0\u00a0 |\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0 |\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0 |\r\n+--------+----------------+-----------------+<\/pre>\n<\/blockquote>\n<p>If you&#8217;re unsure why, you should read more on three-valued logic in SQL. To solve the above, simply use <strong>IS NOT TRUE<\/strong>:<\/p>\n<blockquote>\n<pre>SELECT @empty, @empty IS NOT TRUE;\r\n+--------+--------------------+\r\n| @empty | @empty IS NOT TRUE |\r\n+--------+--------------------+\r\n| NULL\u00a0\u00a0 |\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1 |\r\n+--------+--------------------+<\/pre>\n<\/blockquote>\n<p>In summary, use <strong>IS TRUE<\/strong> and <strong>IS NOT TRUE<\/strong> so as to normalize truth values into a <strong>0<\/strong>, <strong>1<\/strong> value range, <strong>C<\/strong> style, including handling of <strong>NULL<\/strong>s.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What makes for a true statement? We usually test statements using a WHERE clause: SELECT * FROM world.City WHERE Population &gt; 1000000 The &#8220;Population &gt; 1000000&#8221; statement makes for a boolean expression. Using WHERE is just one way of evaluating it. One can also test with IF(): SET @val := 7; SELECT IF(@val &gt; 2, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[5],"tags":[21,20],"class_list":["post-4198","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-sql","tag-syntax"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2bZZp-15I","_links":{"self":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts\/4198","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/comments?post=4198"}],"version-history":[{"count":11,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts\/4198\/revisions"}],"predecessor-version":[{"id":4638,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts\/4198\/revisions\/4638"}],"wp:attachment":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/media?parent=4198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/categories?post=4198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/tags?post=4198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}