{"id":1334,"date":"2016-01-18T10:05:55","date_gmt":"2016-01-18T02:05:55","guid":{"rendered":"http:\/\/www.821121.com\/?p=1334"},"modified":"2021-01-07T15:11:00","modified_gmt":"2021-01-07T07:11:00","slug":"%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0%e4%b9%8bsql-table-joins-1","status":"publish","type":"post","link":"http:\/\/www.821121.com\/?p=1334","title":{"rendered":"\u5b66\u4e60\u7b14\u8bb0\u4e4bSQL-Table Joins 1"},"content":{"rendered":"<h1>Table Joins<\/h1>\n<p><!--more--><\/p>\n<p>Table joins are set operations (\u96c6\u5408\u8fd0\u7b97); they are not merely to retrieve data by using the FKs defined on the tables.<\/p>\n<h3>Join VS Loop Query<\/h3>\n<p>The \u201cLoop Query\u201d means using \u201cprocedural processing logic\u201d to replace the table join operation. It will first query the data from one table and then use the results (a list of constant values) to probe the final result from the other table in one loop.<br \/>\nFor example, the SQL statement (using table join) <\/p>\n<pre><code class=\"language-sql\">SELECT t1.col1, t2.col2 FROM tab1 t1, tab2 t2 WHERE t1.key# = t2.join_field;<\/code><\/pre>\n<p>\u2026can be rewritten by using \u201cLoop Query\u201d like below\u2026 <\/p>\n<pre><code class=\"language-sql\">FOR rec in (SELECT key#, col1 FROM tab1) LOOP SELECT col2 FROM tab2 WHERE join_field = rec.key#; END LOOP;<\/code><\/pre>\n<p>If the SQL (table join) statement involves some operations (like order by, group by, etc) which makes the SQL cannot return the first set of results before processing the whole data set, the \u201cloop query\u201d might performs better than Join sometimes. However, we can rewrite the general table join by taking advantage of some techniques (like \u201cinline view\u201d, \u201cscalar sub-query\u201d, etc), which can make the table join performs well.<br \/>\nExample 1:<\/p>\n<pre><code class=\"language-sql\">SELECT a.fld1, \u2026\u2026, b.col1, \u2026.. FROM tab2 b, tab1 a WHERE a.key1 = b.key2 AND a.fld1 = &#039;10&#039; ORDER BY a.fld2<\/code><\/pre>\n<p>Can be rewritten using inline view as follows, <\/p>\n<pre><code class=\"language-sql\">SELECT x.fld1, \u2026., x.fldn, y.col1\u2026.., y.coln FROM (SELECT fld1, \u2026.., fldn FROM tab1 WHERE fld = &#039;10&#039; ORDER BY fld2) x, Tab2 y WHERE y.key2 = x.key1<\/code><\/pre>\n<p>Example 2: <\/p>\n<pre><code class=\"language-sql\">SELECT b.dept_name, sum(a.sale_money) FROM tab1 a, tab2 b WHERE a.dept# = b.dept# AND a.sale_date like &#039;200503%&#039; GROUP BY b.dept#<\/code><\/pre>\n<p>Can be rewritten as follows\u2026 <\/p>\n<pre><code class=\"language-sql\">SELECT x.dept#, y.dept_name, sale_money FROM (SELECT dept#, sum(sale_money) sale_money FROM tab1 WHERE sale_date like &#039;200503%&#039; GROUP BY dept#) x, TAB2 y WHERE y.dept# = x.dept#<\/code><\/pre>\n<p>Example 3: <\/p>\n<pre><code class=\"language-sql\">SELECT a.*, decode(a.type, \u20181\u2019, b.client_name, \u20182\u2019, project_name) name FROM tab a, clients b, projects c WHERE a.issue_date like &#039;200503%&#039; AND b.client_no(+) = decode(a.type, \u20181\u2019, a.type_code) AND c.project_no(+) = decode(a.type, \u20182\u2019, a.type_code)<\/code><\/pre>\n<p>Can be rewritten using scalar sub-query as follows\u2026<\/p>\n<pre><code class=\"language-sql\">SELECT a.*, (SELECT client_name FROM clients b WHERE b.client_no = a.type_code), (SELECT project_name FROM projects c WHERE c.project_no = a.type_code) FROM tab a WHERE a.issue_date like &#039;200503%&#039;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Table Joins<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,48],"tags":[],"class_list":["post-1334","post","type-post","status-publish","format-standard","hentry","category-oracle-11g","category-sql"],"_links":{"self":[{"href":"http:\/\/www.821121.com\/index.php?rest_route=\/wp\/v2\/posts\/1334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.821121.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.821121.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.821121.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.821121.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1334"}],"version-history":[{"count":1,"href":"http:\/\/www.821121.com\/index.php?rest_route=\/wp\/v2\/posts\/1334\/revisions"}],"predecessor-version":[{"id":1335,"href":"http:\/\/www.821121.com\/index.php?rest_route=\/wp\/v2\/posts\/1334\/revisions\/1335"}],"wp:attachment":[{"href":"http:\/\/www.821121.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.821121.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1334"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.821121.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}