sql UNION 和UNION ALL 数据连接查询

UNION 和UNION ALL 数据连接查询

UNION 和UNION ALL 都表示将2个数据集拼接(说白了就是将后面查询的结果拼接到前面查询结果的尾部),但union是去掉重复的和distinct一样union all 是不去重的。

PS:2个查询结果集的字段名称和每个字段的类型要保持一致 不要一个int类型一个varchar类型

select `Id`,`Name` from  Student union select `Id`,CourseName `Name` from  Course 

select `Id`,`Name` from  Student union all select `Id`,CourseName `Name` from  Course 
原文地址:https://www.cnblogs.com/YorkQi/p/14801620.html