UNION 和 UNION ALL 有什么区别? [英] What is the difference between UNION and UNION ALL?

查看:51
本文介绍了UNION 和 UNION ALL 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UNIONUNION ALL 有什么区别?

推荐答案

UNION 删除重复记录(结果中的所有列都相同),UNION ALL 会不是.

UNION removes duplicate records (where all columns in the results are the same), UNION ALL does not.

使用 UNION 而不是 UNION ALL 会降低性能,因为数据库服务器必须做额外的工作来删除重复的行,但通常你不希望重复项(尤其是在开发报告时).

There is a performance hit when using UNION instead of UNION ALL, since the database server must do additional work to remove the duplicate rows, but usually you do not want the duplicates (especially when developing reports).

SELECT 'foo' AS bar UNION SELECT 'foo' AS bar

结果:

+-----+
| bar |
+-----+
| foo |
+-----+
1 row in set (0.00 sec)

联合所有示例:

SELECT 'foo' AS bar UNION ALL SELECT 'foo' AS bar

结果:

+-----+
| bar |
+-----+
| foo |
| foo |
+-----+
2 rows in set (0.00 sec)

这篇关于UNION 和 UNION ALL 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆