{"id":5445,"date":"2012-11-05T20:22:15","date_gmt":"2012-11-05T18:22:15","guid":{"rendered":"http:\/\/code.openark.org\/blog\/?p=5445"},"modified":"2012-11-05T20:22:15","modified_gmt":"2012-11-05T18:22:15","slug":"pop-quiz-funny-syntax","status":"publish","type":"post","link":"https:\/\/code.openark.org\/blog\/mysql\/pop-quiz-funny-syntax","title":{"rendered":"Pop quiz: funny syntax"},"content":{"rendered":"<p>The following questions are of little importance, yet I find them entertaining. I stumbled upon these while developing QueryScript.<\/p>\n<p>Can you guess the results of the following statements?<\/p>\n<h4>Pop quiz 1<\/h4>\n<blockquote>\n<pre>SET @x := 7;\r\nSELECT ++@x;<\/pre>\n<\/blockquote>\n<p>What is the computation result? What will be the value of <strong>@x<\/strong>?<\/p>\n<h4>Pop quiz 2<\/h4>\n<blockquote>\n<pre>SET @ := 4;\r\nSELECT @ + @'' + @``<\/pre>\n<\/blockquote>\n<p>What is the computation result?<\/p>\n<h4>Pop quiz 3<\/h4>\n<blockquote>\n<pre>SET @a := 2;\r\nSELECT @a = @'a' = @`a`<\/pre>\n<\/blockquote>\n<p>Do we get <strong>TRUE<\/strong> or <strong>FALSE<\/strong>? When?<!--more--><\/p>\n<h4>Hints<\/h4>\n<p>Consider the following queries as hints to the above questions:<\/p>\n<blockquote>\n<pre>SELECT +++++-@x;\r\nSELECT @ = @'', @ = @``\r\nSELECT (@a = @'a') = @`a`<\/pre>\n<\/blockquote>\n<h4>Answers<\/h4>\n<ul>\n<li><strong>Pop quiz 1<\/strong><\/li>\n<\/ul>\n<p style=\"padding-left: 30px;\"><strong>++@x<\/strong> is interpreted as <strong>+(+(@x))<\/strong>, which is in turn evaluated as <strong>0+(0+(@x))<\/strong>. No relation to <strong>C<\/strong>&#8216;s <strong>++<\/strong> operator. <strong>@x<\/strong> is unchanged.<\/p>\n<ul>\n<li><strong>Pop quiz 2<\/strong><\/li>\n<\/ul>\n<p style=\"padding-left: 30px;\"><strong>@x<\/strong> is the same as <strong>@&#8217;x&#8217;<\/strong> and as <strong>@`x`<\/strong>. What&#8217;s funny is that one is allowed to create the empty-named user defined variable <strong>@&#8221;<\/strong>. Makes for a weird looking syntax, but nothing special about it. Computation result is <strong>12<\/strong>.<\/p>\n<ul>\n<li><strong>Pop quiz 3<\/strong><\/li>\n<\/ul>\n<p style=\"padding-left: 30px;\">We&#8217;ve already established that <strong>@a<\/strong>, <strong>@&#8217;a&#8217;<\/strong> and <strong>@`a`<\/strong> are the same, I just used this notation for adding pepper to the quiz. The real question is whether <strong>2 = 2 = 2<\/strong> holds true.\u00a0 It does not. There is no 3-way comparison. All comparisons are in pairs, which is why the expression evaluates as <strong>(2 = 2) = 2<\/strong>, leading to <strong>1 = 2<\/strong>, <strong>&#8220;1&#8221;<\/strong> being the <strong>TRUE<\/strong> value of <strong>(2 = 2)<\/strong>. The only value of <strong>@a<\/strong> for which the expression holds true is <strong>1<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The following questions are of little importance, yet I find them entertaining. I stumbled upon these while developing QueryScript. Can you guess the results of the following statements? Pop quiz 1 SET @x := 7; SELECT ++@x; What is the computation result? What will be the value of @x? Pop quiz 2 SET @ := [&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-5445","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-1pP","_links":{"self":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts\/5445","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=5445"}],"version-history":[{"count":28,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts\/5445\/revisions"}],"predecessor-version":[{"id":5767,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts\/5445\/revisions\/5767"}],"wp:attachment":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/media?parent=5445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/categories?post=5445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/tags?post=5445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}