Postgresql UNION & UNION ALL 用法以及应用场景.md
来源:互联网 发布:淘宝怎么修改分类 编辑:程序博客网 时间:2024/05/21 09:34
想必都接触过联合查询 Union 吧,那么 Postgresql里,union 如何使用呢 ?
Union又有哪些应用场景呢??
废话少说,show me your demo
//其实Union可以适应各种花式查询,比如:1. 我想要一张表的前三条记录和名字为"张三"的记录。//查询所有记录:postgres=# select * from t_union ; name ----------- killerbee naruto frank dancer 张三(5 rows)//??想想这里为嘛会错误?? 提示语法错误。答案就是:少了 括号() ,请注意sql语法。postgres=# select * from t_union limit 3 union select * from t_union where name=' 张三' ;ERROR: syntax error at or near "union"LINE 1: select * from t_union limit 3 union select * from t_union wh... ^//正确答案:postgres=# (select * from t_union limit 3) union select * from t_union where name='张三' ; name ----------- 张三 killerbee frank naruto(4 rows)
- union 和 union all 的区别: // 不带 all postgres=# select * from t_union union select * from t_union ; name ----------- naruto frank dancer 张三 killerbee(5 rows)//带all postgres=# select * from t_union union all select * from t_union ; name ----------- killerbee naruto frank dancer 张三 killerbee naruto frank dancer 张三(10 rows)
- 问题来了,区别在哪里?? 想必你也猜出来了:
//区别在这里:
The PostgreSQL UNION clause/operator is used to combine the results of two or more SELECT statements without returning any duplicate rows.To use UNION, each SELECT must have the same number of columns selected, the same number of column expressions, the same data type, and have them in the same order but they do not have to be the same length.
The UNION ALL operator is used to combine the results of two SELECT statements including duplicate rows. The same rules that apply to UNION apply to the UNION ALL operator as well.
所以效果上的区别就是: union过滤相同行, union all 不过滤相同行。 自然前者开销大,较慢,甚至有时候还会用到磁盘进行排序, 而后者较快。
具体还是得看需求需求!!
// union 在生产中还有各种各种样的花式查询,以下需求来自互联网:
- 应用场景:
1.最常见的是过程表与历史表UNION
2.相同数据表,来至不同数据源的UNION数据统计。
3.有时候利用union可以解决一些奇怪的判断语句.比如将报表的合计一起返回
4.之前使用的例子,有多个信息模块的数据,需要展示,每个模块表都有一个title,id,picture字段。为减少多次的查询SQL,使用union将这些表的数据合为一个结果集返回。
- Postgresql UNION & UNION ALL 用法以及应用场景.md
- PostgreSql-UNION和UNION ALL的用法与差异
- union/union all的用法
- union/union all的用法
- Union all 和 Union用法
- union, union all 的用法
- PostgreSQL UNION[ALL],INTERSECT [ALL],EXCEPT [ALL]
- union all 用法
- MySQL union(all) 用法
- MYSQL用法(三) UNION,UNION ALL 用法
- sql 之 union && union all 以及安全问题
- select 中的union 和union all用法
- SQL Union和SQL Union All用法
- SQL Union和SQL Union All用法
- SQL Union和Union All用法
- SQL Union和SQL Union All用法
- select 中的union 和union all用法
- SQL Union和SQL Union All用法
- AngularJS 最常用的八种功能
- biTStream功能介绍与应用
- React component 生命周期
- 计算数字k在0到n中的出现的次数,k可能是0~9的一个值
- 产品汪的日常
- Postgresql UNION & UNION ALL 用法以及应用场景.md
- 编写代码时,每打一个字符就会弹出“Computing additional info”.Could not initialize class……
- 二维码自动扫描完整版
- Android中mpchartlib折线图的详细属性以及实现(双曲线)
- PHP7源码安装Memcached和Memcache拓展
- (译) 在AngularJS中使用的表单验证功能
- mac 更改文件权限
- 题目1112:拦截导弹
- 寻找一个字符串中第一个只出现两次的字符