📜  SQL中JOIN和UNION的区别

📅  最后修改于: 2021-09-15 01:58:05             🧑  作者: Mango

加入
SQL 中的 JOIN 用于根据多个表之间的匹配条件组合来自多个表的数据。使用 JOIN 语句将数据组合到新列中。

考虑两个表:

男孩们

女孩们

例子:

sql> 
SELECT Boys.Name, Boys.Age, Girls.Address,
FROM Boys 
INNER JOIN Girls 
ON Boys.Rollno = Girls.Rollno; 

结果表是:

联合
SQL 中的 UNION 用于组合两个或多个 SELECT 语句的结果集。使用 UNION 语句组合的数据将结果转化为新的不同行。

例子:

sql> 
SELECT Name 
FROM Boys 
WHERE Rollno < 16 

UNION

SELECT Name 
FROM Girls 
WHERE Rollno > 9 

结果表是:

SQL 中 JOIN 和 UNION 的区别:

JOIN UNION
JOIN combines data from many tables based on a matched condition between them. SQL combines the result-set of two or more SELECT statements.
It combines data into new columns. It combines data into new rows
Number of columns selected from each table may not be same. Number of columns selected from each table should be same.
Datatypes of corresponding columns selected from each table can be different. Datatypes of corresponding columns selected from each table should be same.
It may not return distinct columns. It returns distinct rows.