我经常将查询结果转换为用户定义的类型。考虑这个简单的例子:
test=# create type test_type as (a int, b int);
CREATE TYPE
test=# create table test_table (a int, b int);
CREATE TABLE
test=# insert into test_table values (1,2);
INSERT 0 1
test=# select r::test_type from (select * from test_table t) as r;
r
-------
(1,2)
(1 row)
对于我的很多查询来说,子查询是必要的,而且效果很好。但是,有时它是从表到类型的简单一对一映射,如上例所示。
有没有更简单的表达方式?
当我尝试对我来说显而易见的事情时,我得到了错误:
test=# select t::test_type from test_table t;
ERROR: cannot cast type test_table to test_type
LINE 1: select t::test_type from test_table t;
^
最佳答案
为此,您可以使用带有通配符列扩展的 ROW
构造。
regress=> select ROW(t.*)::testtype from testtable t;
row
-------
(1,2)
(1 row)
关于postgresql - 有没有更简洁的方法将查询结果转换为类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24918235/