What is the difference between JOIN
and UNION
? Can I have an example?
相关问题
- SQL join to get the cartesian product of 2 columns
- SQL join to get the cartesian product of 2 columns
- sql execution latency when assign to a variable
- Difference between Types.INTEGER and Types.NULL in
- php PDO::FETCH_ASSOC doesnt detect select after ba
Union Operation is combined result of the Vertical Aggregate of the rows, Union Operation is combined result of the Horizontal Aggregate of the Columns.
Any modern DBS, like MariaDB, implements a UNION JOIN command. This is an SQL 3 command, but it isn't well known or used. Do learn more on UNION JOIN.
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:
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.You may see the same schematic explanations for both, but these are totally confusing.
For UNION:
For JOIN:
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.