pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。
UNION 结构有些不同,它必须把可能不相似的类型 匹配起来构成单一结果集。
UNION 求值
检查所有结果的类型是否一致。
把来自各 UNION 子句的每个结果强制转换成与第一个 SELECT 子句或目标列相同的类型。
tgl=> SELECT text 'a' AS "Text" UNION SELECT 'b'; Text ------ a b (2 rows)
tgl=> SELECT 1.2 AS "Float8" UNION SELECT 1;
Float8
--------
1
1.2
(2 rows)
union 的类型被强制匹配 union 中第一个/顶端子句的类型:
tgl=> SELECT 1 AS "All integers"
tgl-> UNION SELECT '2.2'::float4
tgl-> UNION SELECT 3.3;
All integers
--------------
1
2
3
(3 rows)
另一种解析器策略可以是选择这一组中的"最佳"类型,但 由于解析器使用了精巧的递归技术,这更加 困难。不过,在选择 into 表时会使用"最佳"类型:
tgl=> CREATE TABLE ff (f float);
CREATE
tgl=> INSERT INTO ff
tgl-> SELECT 1
tgl-> UNION SELECT '2.2'::float4
tgl-> UNION SELECT 3.3;
INSERT 0 3
tgl=> SELECT f AS "Floating point" from ff;
Floating point
------------------
1
2.20000004768372
3.3
(3 rows)
译文有误、术语不当或页面显示问题,请到译文仓库 pgsty/pgdoc 报告译文问题。 英文原文本身的问题,请在当前版本的对应页面向上游反馈;上游不再修订已结束维护的版本。