在 postgres 中,当我尝试合并两个表时出现错误,其中一个表的列 (Amount
) 包含 double precision
数据类型,并且其他表没有匹配的列,我希望该表中的记录在 Amount
字段中只有 NULL
。
错误:
"union types text and double precision cannot be matched postgres"
伪代码:
SELECT * FROM (
SELECT
t1.Amount AS 'amount',
NULL::DATE AS 'date'
FROM Table1 AS t1
UNION ALL
SELECT
/* next line is the issue */
NULL AS 'amount',
t2.Date AS 'date'
FROM Table2 AS t2
) AS FOO
我相当确定这个解决方案是一个简单的转换问题,但无法通过搜索找到任何内容。我如何在 postgres 中执行与 NULL::DOUBLE
等效的操作?
编辑::后代
@klin 和@a_horse_with_no_name 的评论接受的答案指向一个“历史”postgres 转换表达式::
,其中 syntax is equivalent :
CAST ( expression AS type )
expression::type
而且,这是一个列表 postgres data types .
最佳答案
在 Postgres 中,double precision
类型也称为 float8
或简称为 float
。使用其中之一。
select null::double precision, null::float;
关于postgresql - UNION ALL,将 NULL 转换为 double ,Postgres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41864212/