hive sql 中的运算
SQL supports few Set operations which can be performed on the table data. These are used to get meaningful results from data stored in the table, under different special conditions.
SQL支持很少的可以对表数据执行的Set操作。 这些用于在不同的特殊条件下从表中存储的数据中获取有意义的结果。
In this tutorial, we will cover 4 different types of SET operations, along with example:
在本教程中,我们将介绍4种不同类型的SET操作以及示例:
UNION
联盟
UNION ALL
全联盟
INTERSECT
相交
MINUS
减去
联盟行动 (UNION Operation)
UNION is used to combine the results of two or more SELECT
statements. However it will eliminate duplicate rows from its resultset. In case of union, number of columns and datatype must be same in both the tables, on which UNION operation is being applied.
UNION用于合并两个或多个SELECT
语句的结果。 但是,它将从结果集中消除重复的行。 如果使用并集,则在两个表上要应用UNION操作的列数和数据类型必须相同。
UNION示例 (Example of UNION)
The First table,
第一张桌子
ID | Name |
---|---|
1 | abhi |
2 | adam |
ID | 名称 |
---|---|
1个 | 阿比 |
2 | 亚当 |
The Second table,
第二张桌子
ID | Name |
---|---|
2 | adam |
3 | Chester |
ID | 名称 |
---|---|
2 | 亚当 |
3 | 切斯特 |
Union SQL query will be,
Union SQL查询将是,
SELECT * FROM First
UNION
SELECT * FROM Second;
The resultset table will look like,
结果集表如下所示:
ID | NAME |
---|---|
1 | abhi |
2 | adam |
3 | Chester |
ID | 名称 |
---|---|
1个 | 阿比 |
2 | 亚当 |
3 | 切斯特 |
全联盟 (UNION ALL)
This operation is similar to Union. But it also shows the duplicate rows.
此操作类似于联合。 但它也显示重复的行。
全部联合示例 (Example of Union All)
The First table,
第一张桌子
ID | NAME |
---|---|
1 | abhi |
2 | adam |
ID | 名称 |
---|---|
1个 | 阿比 |
2 | 亚当 |
The Second table,
第二张桌子
ID | NAME |
---|---|
2 | adam |
3 | Chester |
ID | 名称 |
---|---|
2 | 亚当 |
3 | 切斯特 |
Union All query will be like,
联合所有查询将是这样,
SELECT * FROM First
UNION ALL
SELECT * FROM Second;
The resultset table will look like,
结果集表如下所示:
ID | NAME |
---|---|
1 | abhi |
2 | adam |
2 | adam |
3 | Chester |
ID | 名称 |
---|---|
1个 | 阿比 |
2 | 亚当 |
2 | 亚当 |
3 | 切斯特 |
相交 (INTERSECT)
Intersect operation is used to combine two SELECT
statements, but it only retuns the records which are common from both SELECT
statements. In case of Intersect the number of columns and datatype must be same.
相交操作用于二者结合起来SELECT
语句,但它只是retuns这是从两个共同记录SELECT
语句。 在相交的情况下,列数和数据类型必须相同。
NOTE: MySQL does not support INTERSECT operator. 注意: MySQL不支持INTERSECT运算符。
相交的例子 (Example of Intersect)
The First table,
第一张桌子
ID | NAME |
---|---|
1 | abhi |
2 | adam |
ID | 名称 |
---|---|
1个 | 阿比 |
2 | 亚当 |
The Second table,
第二张桌子
ID | NAME |
---|---|
2 | adam |
3 | Chester |
ID | 名称 |
---|---|
2 | 亚当 |
3 | 切斯特 |
Intersect query will be,
相交查询将是
SELECT * FROM First
INTERSECT
SELECT * FROM Second;
The resultset table will look like
结果集表如下所示
ID | NAME |
---|---|
2 | adam |
ID | 名称 |
---|---|
2 | 亚当 |
减去 (MINUS)
The Minus operation combines results of two SELECT
statements and return only those in the final result, which belongs to the first set of the result.
减号运算组合两个SELECT
语句的结果,并且仅返回最终结果(属于结果的第一组)中的结果。
减号示例 (Example of Minus)
The First table,
第一张桌子
ID | NAME |
---|---|
1 | abhi |
2 | adam |
ID | 名称 |
---|---|
1个 | 阿比 |
2 | 亚当 |
The Second table,
第二张桌子
ID | NAME |
---|---|
2 | adam |
3 | Chester |
ID | 名称 |
---|---|
2 | 亚当 |
3 | 切斯特 |
Minus query will be,
减号查询将是,
SELECT * FROM First
MINUS
SELECT * FROM Second;
The resultset table will look like,
结果集表如下所示:
ID | NAME |
---|---|
1 | abhi |
ID | 名称 |
---|---|
1个 | 阿比 |
翻译自: https://www.studytonight.com/dbms/set-operation-in-sql.php
hive sql 中的运算