[SQL挖掘机] - 多表连接: union

介绍:

sql中的union是用于合并两个或多个select语句的结果集的操作符。它将多个查询的结果合并成一个结果集,并自动去除重复的行。请注意,union操作要求被合并的查询返回相同数量和类型的列。

用法:

union的基本语法如下:

select_statement1
union
select_statement2;

其中,select_statement1select_statement2是两个或多个select语句,它们的结果将被合并。

举例:

假设我们有两个表,table1table2,它们结构相同,都有nameage列。我们可以使用union将它们的数据合并:

-- 从 table1 和 table2 表中选择 name 和 age 列,并合并结果集
select name, age from table1
union
select name, age from table2;

注意:如果希望保留所有的行,包括重复行,可以使用union all,而不是unionunion all可以看下一篇的介绍~

猜你喜欢

转载自blog.csdn.net/qq_40249337/article/details/131968989