> 文档中心 > MySQL union 和 union all 的区别与使用

MySQL union 和 union all 的区别与使用


MySQL union 和 union all 的区别与使用

相同点:

1.union 和 union all 都是对多个select 查询结果进行合并

2.每个select查询的结果的列数必须一致;字段名可以不一样,但是属性是一样的,只有这样才能合并啊

区别:

显示结果:union合并后的结果已经做了去重的;union all合并后的结果只是做了多个查询语句的并集

性能:

union会根据字段的顺序进行排序;union all 只是对多个查询结果进行合并然后返回。从效率上说:union all要比union 快很多;因此,如果要对结果去重就使用union,不要去重的话就使用union all。

使用:

#union使用,我只用两个select语句举例子;也可拼接很多select语句select name from admin_userunion select name from client;
#union all 使用;使用两个select语句举例子;(union all也可以拼接很多select语句)select name from admin_userunion all select name from client;