doc-exports/docs/dli/sqlreference/dli_08_0183.html
Su, Xiaomeng 04d4597cf3 dli_sqlreference_0511_version
Reviewed-by: Pruthi, Vineet <vineet.pruthi@t-systems.com>
Co-authored-by: Su, Xiaomeng <suxiaomeng1@huawei.com>
Co-committed-by: Su, Xiaomeng <suxiaomeng1@huawei.com>
2023-11-02 14:34:08 +00:00

28 lines
3.6 KiB
HTML

<a name="dli_08_0183"></a><a name="dli_08_0183"></a>
<h1 class="topictitle1">UNION</h1>
<div id="body8662426"><div class="section" id="dli_08_0183__en-us_topic_0093946951_sad057f3cc51e45dcbe8d7d1bd03f7308"><h4 class="sectiontitle">Function</h4><p id="dli_08_0183__en-us_topic_0093946951_a2aa5ea4335fd4603a2ba3816a2d48c1f">This statement is used to return the union set of multiple query results.</p>
</div>
<div class="section" id="dli_08_0183__en-us_topic_0093946951_s967e1de553f94949acd33b083cf35ac8"><h4 class="sectiontitle">Syntax</h4><div class="codecoloring" codetype="Sql" id="dli_08_0183__screen1814420308449"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="n">select_statement</span><span class="w"> </span><span class="k">UNION</span><span class="w"> </span><span class="p">[</span><span class="k">ALL</span><span class="p">]</span><span class="w"> </span><span class="n">select_statement</span><span class="p">;</span>
</pre></div></td></tr></table></div>
</div>
</div>
<div class="section" id="dli_08_0183__en-us_topic_0093946951_sc5d05e34b8224d128d8927b953090a32"><h4 class="sectiontitle">Keyword</h4><p id="dli_08_0183__en-us_topic_0093946951_a647813ec71ce4feda214976a8dd2a659">UNION: The set operation is used to join the head and tail of a table based on certain conditions. The number of columns returned by each SELECT statement must be the same. The column type and column name may not be the same.</p>
</div>
<div class="section" id="dli_08_0183__en-us_topic_0093946951_s6116c9ed96174568bd75a62bf7988b32"><h4 class="sectiontitle">Precautions</h4><ul id="dli_08_0183__en-us_topic_0093946951_u1638f0ad1f0f4d1ba2c1aea5fac7e899"><li id="dli_08_0183__en-us_topic_0093946951_l8f6a7db2d22b41d7be326e73b790ea06">By default, the repeated records returned by UNION are removed. The repeated records returned by UNION ALL are not removed.</li><li id="dli_08_0183__en-us_topic_0093946951_le3c218698e5c4a22a11b75f13a927819">Do not add brackets between multiple set operations, such as UNION, INTERSECT, and EXCEPT. Otherwise, an error is reported. </li></ul>
</div>
<div class="section" id="dli_08_0183__en-us_topic_0093946951_sa1d99b07f75344e0bc8dc597902bc574"><h4 class="sectiontitle">Example</h4><p id="dli_08_0183__en-us_topic_0093946951_a7c01186850d543f5baae2e5d04975d6a">To return the union set of the query results of the <strong id="dli_08_0183__b595989378">SELECT * FROM student _1</strong> and <strong id="dli_08_0183__b810318153714">SELECT * FROM student _2</strong> commands with the repeated records removed, run the following statement:</p>
<div class="codecoloring" codetype="Sql" id="dli_08_0183__screen5341124744419"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">student_1</span><span class="w"> </span><span class="k">UNION</span><span class="w"> </span><span class="k">SELECT</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">student_2</span><span class="p">;</span>
</pre></div></td></tr></table></div>
</div>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="dli_08_0182.html">Set Operations</a></div>
</div>
</div>