JOIN 和 UNION 有什么区别?
JOIN
和 UNION
有什么区别? 我可以举个例子吗?
What is the difference between JOIN
and UNION
? Can I have an example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
JOIN
和 UNION
有什么区别? 我可以举个例子吗?
What is the difference between JOIN
and UNION
? Can I have an example?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(14)
UNION
将查询中的行依次放置,而JOIN
生成笛卡尔积并对其进行子集化——完全不同的操作。UNION
的简单示例:JOIN
的类似简单示例:UNION
puts lines from queries after each other, whileJOIN
makes a cartesian product and subsets it -- completely different operations. Trivial example ofUNION
:similary trivial example of
JOIN
:UNION 将两个或多个查询的结果合并为包含属于联合中所有查询的所有行的单个结果集。
通过使用 JOIN,您可以从两个或多个基于表之间逻辑关系的表。 连接指示 SQL 应如何使用一个表中的数据来选择另一表中的行。
UNION 操作与使用组合两个表中的列的 JOIN 不同。
UNION 示例:
输出:
JOIN 示例:
这将输出两个表中条件
a.Id = b.AFKId
为 true 的所有行。UNION combines the results of two or more queries into a single result set that includes all the rows that belong to all queries in the union.
By using JOINs, you can retrieve data from two or more tables based on logical relationships between the tables. Joins indicate how SQL should use data from one table to select the rows in another table.
The UNION operation is different from using JOINs that combine columns from two tables.
UNION Example:
Output:
JOIN Example:
This will output all the rows from both the tables for which the condition
a.Id = b.AFKId
is true.您可能会看到两者相同的示意性解释,但这些完全令人困惑。
对于 UNION:
对于加入:
You may see the same schematic explanations for both, but these are totally confusing.
For UNION:
For JOIN:
JOIN:
连接用于显示具有相同或相同的列
不同表的不同名称。 显示的输出
将单独显示所有列。 那就是
列将彼此相邻对齐。
UNION:
UNION 集合运算符用于组合两个数据
具有相同数据类型的列的表。
当执行 UNION 时,两个表中的数据将
收集在具有相同数据类型的单个列中。
例如:
请参阅下面所示的两个表:
现在,为了执行 JOIN 类型,查询如下所示。
那是一个加入。
UNION 意味着您必须将表或结果集与
相同数量和类型的列,然后将其添加到
表/结果集在一起。 看这个例子:
JOIN:
A join is used for displaying columns with the same or
different names from different tables. The output displayed
will have all the columns shown individually. That is, the
columns will be aligned next to each other.
UNION:
The UNION set operator is used for combining data from two
tables which have columns with the same datatype.
When a UNION is performed the data from both tables will be
collected in a single column having the same datatype.
For example:
See the two tables shown below:
Now for performing a JOIN type the query is shown below.
That is a join.
UNION means that you have to tables or resultset with the
same amount and type of columns and you add this to
tables/resultsets together. Look at this example:
它们是完全不同的东西。
联接允许您关联不同表中的相似数据。
联合将两个不同查询的结果作为单个记录集返回。
They're completely different things.
A join allows you to relate similar data in different tables.
A union returns the results of two different queries as a single recordset.
连接和并集可用于组合来自一个或多个表的数据。 区别在于数据的组合方式。
简单来说,连接将数据组合到新列中。 如果两个表连接在一起,则第一个表中的数据将显示在同一行中第二个表的列旁边的一组列中。
联合将数据组合成新行。 如果两个表“联合”在一起,则第一个表中的数据位于一组行中,第二个表中的数据位于另一组行中。 这些行具有相同的结果。
Joins and unions can be used to combine data from one or more tables. The difference lies in how the data is combined.
In simple terms, joins combine data into new columns. If two tables are joined together, then the data from the first table is shown in one set of column alongside the second table’s column in the same row.
Unions combine data into new rows. If two tables are “unioned” together, then the data from the first table is in one set of rows, and the data from the second table in another set. The rows are in the same result.
Union 使两个查询看起来像一个。 连接用于在单个查询语句中检查两个或多个表
Union makes two queries look like one. Joins are for examining two or more tables in a single query statement
请记住,联合将合并结果(SQL Server 是肯定的)(功能还是错误?)
id,值
1,3
id,值,id,值
1,3,1,3
Remember that union will merge results (SQL Server to be sure)(feature or bug?)
id,value
1,3
id,value,id,value
1,3,1,3
SQL JOIN 子句用于组合数据库中两个或多个表中的记录。 JOIN 是一种通过使用每个表通用的值来组合两个表中的字段的方法。
SQL UNION 运算符组合两个或多个 SELECT 语句的结果。
UNION 中的每个 SELECT 语句必须具有相同的列数。 这些列还必须具有相似的数据类型。 此外,每个 SELECT 语句中的列必须具有相同的顺序。
例如:
表 1 客户/表 2 订单
内连接:
并集:
The SQL JOIN clause is used to combine records from two or more tables in a database. A JOIN is a means for combining fields from two tables by using values common to each.
The SQL UNION operator combines the result of two or more SELECT statements.
Each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types. Also, the columns in each SELECT statement must be in the same order.
For example:
table 1 customers / table 2 orders
Inner join:
Union:
联合运算是行的垂直聚合的组合结果,
连接操作是列的水平聚合的组合结果。
Union Operation is combined result of the Vertical Aggregate of the rows,
Join Operation is combined result of the Horizontal Aggregate of the Columns.
使用 UNION
UNION 将两个或多个查询的结果组合成一个结果集,其中包括属于联合中所有查询的所有行。
使用 JOIN 连接
,您可以根据表之间的逻辑关系从两个或多个表中检索数据。
Ussing UNION
UNION is combines the results of two or more queries into a single result set that includes all the rows that belong to all queries in the union.
Ussing JOINs
JOINs, you can retrieve data from two or more tables based on logical relationships between the tables.
UNION 运算符仅用于组合两个或多个 SELECT 语句。
JOIN 用于从每个表中选择行,可以通过内部、外部、左或右方法。
请参阅此处和此处 。 有更好的示例解释。
The UNION operator is just for combining two or more SELECT statements.
While JOIN is for selecting rows from each table, either by the inner, outer, left or right method.
Refer to here and here . There is a better explanation with examples.
从抽象上讲,它们是相似的,因为两个表或结果集被组合,但 UNION 实际上用于将结果集与相同数量的列和具有相似数据类型的列组合起来。 结构是相同的,只是添加了新行。
在联接中,您可以将表/结果集与任何可能的结构组合起来,包括没有共享/相似列的笛卡尔联接。
In the abstract, they are similar, in that two tables or result sets are being combined , but UNION is really for combining result sets with the SAME NUMBER OF COLUMNS with the COLUMNS HAVING SIMILAR DATA TYPES. The STRUCTURE is the same, only new rows are being added.
In joins, you can combine tables/result sets with any possible structure, including a cartesian join where there are NO shared/similar columns.
我喜欢将一般差异视为:
I like to think of the general difference as being: