<?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>Gary Pendergast &#187; precedence</title> <atom:link href="http://pento.net/tag/precedence/feed/" rel="self" type="application/rss+xml" /><link>http://pento.net</link> <description>I&#039;m on the Internet</description> <lastBuildDate>Thu, 17 May 2012 04:24:17 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.2</generator> <cloud
domain='pento.net' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' /> <item><title>JOIN and comma precedence</title><link>http://pento.net/2009/04/03/join-and-comma-precedence/</link> <comments>http://pento.net/2009/04/03/join-and-comma-precedence/#comments</comments> <pubDate>Fri, 03 Apr 2009 04:00:53 +0000</pubDate> <dc:creator>Gary</dc:creator> <category><![CDATA[MySQL]]></category> <category><![CDATA[comma]]></category> <category><![CDATA[join]]></category> <category><![CDATA[precedence]]></category> <guid
isPermaLink="false">http://pento.net/?p=149</guid> <description><![CDATA[Here&#8217;s a little something that might trip you up occasionally. Have a look at this test scenario: USE test; DROP TABLE IF EXISTS a; DROP TABLE IF EXISTS b; DROP TABLE IF EXISTS c; &#160; CREATE TABLE a &#40; a INT &#41;; CREATE TABLE b &#40; b INT &#41;; CREATE TABLE c &#40; c INT [...]]]></description> <content:encoded><![CDATA[<p>Here&#8217;s a little something that might trip you up occasionally. Have a look at this test scenario:</p><div
class="wp_syntax"><div
class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">USE</span> test<span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">DROP</span> <span style="color: #990099; font-weight: bold;">TABLE</span> <span style="color: #009900;">IF</span> <span style="color: #990099; font-weight: bold;">EXISTS</span> a<span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">DROP</span> <span style="color: #990099; font-weight: bold;">TABLE</span> <span style="color: #009900;">IF</span> <span style="color: #990099; font-weight: bold;">EXISTS</span> b<span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">DROP</span> <span style="color: #990099; font-weight: bold;">TABLE</span> <span style="color: #009900;">IF</span> <span style="color: #990099; font-weight: bold;">EXISTS</span> c<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> a <span style="color: #FF00FF;">&#40;</span> a <span style="color: #999900; font-weight: bold;">INT</span> <span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> b <span style="color: #FF00FF;">&#40;</span> b <span style="color: #999900; font-weight: bold;">INT</span> <span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> c <span style="color: #FF00FF;">&#40;</span> c <span style="color: #999900; font-weight: bold;">INT</span> <span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">SELECT</span> a.a <span style="color: #990099; font-weight: bold;">FROM</span> a <span style="color: #000099;">LEFT</span> <span style="color: #990099; font-weight: bold;">JOIN</span> c <span style="color: #990099; font-weight: bold;">ON</span> c.c <span style="color: #CC0099;">=</span> a.a<span style="color: #000033;">;</span> <span style="color: #808080; font-style: italic;">-- Q1</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> a.a <span style="color: #990099; font-weight: bold;">FROM</span> a<span style="color: #000033;">,</span> b <span style="color: #000099;">LEFT</span> <span style="color: #990099; font-weight: bold;">JOIN</span> c <span style="color: #990099; font-weight: bold;">ON</span> c.c <span style="color: #CC0099;">=</span> a.a<span style="color: #000033;">;</span> <span style="color: #808080; font-style: italic;">-- Q2</span></pre></div></div><p>Q1 and Q2 will produce the same result, right? Wrong! As of MySQL 5.0.12, per the SQL standard, JOIN has had higher precedence than comma &#8216;,&#8217;.</p><p>So, you get the following:</p><div
class="wp_syntax"><div
class="code"><pre class="mysql" style="font-family:monospace;">mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">SELECT</span> a.a <span style="color: #990099; font-weight: bold;">FROM</span> a <span style="color: #000099;">LEFT</span> <span style="color: #990099; font-weight: bold;">JOIN</span> c <span style="color: #990099; font-weight: bold;">ON</span> c.c <span style="color: #CC0099;">=</span> a.a<span style="color: #000033;">;</span>
Empty <span style="color: #990099; font-weight: bold;">set</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">SELECT</span> a.a <span style="color: #990099; font-weight: bold;">FROM</span> a<span style="color: #000033;">,</span> b <span style="color: #000099;">LEFT</span> <span style="color: #990099; font-weight: bold;">JOIN</span> c <span style="color: #990099; font-weight: bold;">ON</span> c.c <span style="color: #CC0099;">=</span> a.a<span style="color: #000033;">;</span>
ERROR <span style="color: #008080;">1054</span> <span style="color: #FF00FF;">&#40;</span>42S22<span style="color: #FF00FF;">&#41;</span>: Unknown <span style="color: #990099; font-weight: bold;">column</span> <span style="color: #008000;">'a.a'</span> <span style="color: #990099; font-weight: bold;">in</span> <span style="color: #008000;">'on clause'</span></pre></div></div><p>This is because, in earlier versions, MySQL interpreted it as ( ( a, b ) LEFT JOIN c ). Now, it interprets this syntax as ( a, ( b LEFT JOIN c ) ). If you run into this problem, the fix is easy. You simply need to add brackets around the table list:</p><div
class="wp_syntax"><div
class="code"><pre class="mysql" style="font-family:monospace;">mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">SELECT</span> a.a <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#40;</span>a<span style="color: #000033;">,</span> b<span style="color: #FF00FF;">&#41;</span> <span style="color: #000099;">LEFT</span> <span style="color: #990099; font-weight: bold;">JOIN</span> c <span style="color: #990099; font-weight: bold;">ON</span> c.c <span style="color: #CC0099;">=</span> a.a<span style="color: #000033;">;</span>
Empty <span style="color: #990099; font-weight: bold;">set</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span></pre></div></div><p>You can read more about this in the <a
title="MySQL Reference Manual: JOIN Syntax" href="http://dev.mysql.com/doc/refman/5.0/en/join.html">MySQL Documentation</a>, from the section starting with &#8220;Previously, the comma&#8221;.</p> ]]></content:encoded> <wfw:commentRss>http://pento.net/2009/04/03/join-and-comma-precedence/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> </channel> </rss>
