简体   繁体   English

oracle数据库中的并集和并集

[英]Union and union all in oracle Database

While executing the below two queries (focus on the part between two asterisks * ____ *), I am really wondering how does the position of UNION ALL changes the output. 在执行以下两个查询时(着重于两个星号* ____ *之间的部分),我真的很想知道UNION ALL的位置如何改变输出。 I am unable to understand. 我听不懂

Query 1 查询1

SELECT 'Jack' AS Name, 100 AS Marks FROM DUAL 
*UNION All SELECT 'Jack' AS Name, 100 AS Marks FROM DUAL*
UNION SELECT 'Jack' AS Name, 100 AS Marks FROM DUAL

Query Result 查询结果

NAME    MARKS
Jack    100

Query 2 查询2

SELECT 'Jack' AS Name, 100 AS Marks FROM DUAL 
UNION SELECT 'Jack' AS Name, 100 AS Marks FROM DUAL 
*UNION ALL SELECT 'Jack' AS Name, 100 AS Marks FROM DUAL*

Query Result 查询结果

NAME    MARKS
Jack    100
Jack    100

Thanks :) 谢谢 :)

If you don't specify parantheses, the select s would get executed one after the other. 如果您不指定括号,则select s将一个接一个地执行。 All set operators minus , union , union all , intersect have the same precedence. 所有集合运算符minusunionunion allintersect都具有相同的优先级。

Oracle Documentation

In the first query, UNION is performed at the end so there would be no dup rows per the query. 在第一个查询中,在最后执行UNION ,因此每个查询都没有重复的行。 In the second, UNION ALL is performed at the end, so there would be dup rows per your query. 在第二步中,在最后执行UNION ALL ,因此每个查询将包含重复行。

Both union and union all have the same precedence as operations. union和union都具有与操作相同的优先级。 So in the absence of parentheses, your two unions would be evaluated from top to bottom. 因此,在没有括号的情况下,您的两个联合将从上至下进行评估。 Your first query is being evaluated like this: 您的第一个查询正在这样评估:

SELECT Name, Marks
FROM
(
    SELECT 'Jack' AS Name, 100 AS Marks FROM DUAL 
    UNION All
    SELECT 'Jack' AS Name, 100 AS Marks FROM DUAL
) t
UNION
SELECT 'Jack' AS Name, 100 AS Marks FROM DUAL

The same reasoning applies to your second query. 同样的道理也适用于您的第二个查询。

The difference between Union and Union all is that Union all will not eliminate duplicate rows, Union和Union all之间的区别是Union all不会消除重复的行,

first query output: 第一个查询输出:

step 1: 第1步:

SELECT 'Jack' AS Name, 100 AS Marks FROM DUAL 
UNION All SELECT 'Jack' AS Name, 100 AS Marks FROM DUAL

result 2 rows because union all allows duplicates. 结果2行,因为union全部允许重复。

step 2: 第2步:

UNION SELECT 'Jack' AS Name, 100 AS Marks FROM DUAL

this query will pick only row without duplicates from the above 2 rows and itself. 该查询将仅选择上述两行及其本身中没有重复的行。 returns 1 row. 返回1行。

in the second query... 在第二个查询中...

step 1 第1步

SELECT 'Jack' AS Name, 100 AS Marks FROM DUAL 
UNION SELECT 'Jack' AS Name, 100 AS Marks FROM DUAL 

returns 1 row because union selects only distinct rows. 返回1行,因为union只选择不同的行。

step 2 第2步

UNION ALL SELECT 'Jack' AS Name, 100 AS Marks FROM DUAL

return 2 rows because it allows duplicates. 返回2行,因为它允许重复。

To understand this behavior,you need to be clear with the difference between UNION and UNION ALL. 若要了解此行为,您需要清楚UNION和UNION ALL之间的区别。

Query 1: Output of first two lines will return 2 rows. 查询1:前两行的输出将返回2行。 However, last line(sql statement) with 'UNION' operator will remove duplicate rows,including it's own output. 但是,使用'UNION'运算符的最后一行(sql语句)将删除重复的行,包括它自己的输出。 So, total first query will return only 1 row. 因此,第一个查询总数将仅返回1行。

Query 2: Now, UNION is on 2nd line. 查询2:现在,UNION在第二行。 After getting combined with 1st line, it will again return only 1 row. 与第一行合并后,它将仅返回第一行。 But, on the 3rd line with 'UNION ALL',it will return 2 rows. 但是,在带有“ UNION ALL”的第三行,它将返回2行。 Because, 'UNION ALL' won't remove duplicates. 因为,“ UNION ALL”不会删除重复项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM