PostgreSQL选择复合元素的数组

Ant*_* F. 4 arrays postgresql composite

我有一个在复合类型上定义的表:

create type footype as (  
a double precision,
b double precision
);  

create table footable as (  
x integer,  
y footype []);
Run Code Online (Sandbox Code Playgroud)

如何在表中包含的复合元素的单个字段上使用select语句?

在此先感谢,
安东尼奥

mu *_*ort 8

只是通常的数组访问语法,后面是通常的复合类型访问语法.所以为演示目的进行了一些设置:

=> insert into footable (x, y) values(1, ARRAY[(1.0,2.0)::footype]);
=> select * from footable;
 x |     y     
---+-----------
 1 | {"(1,2)"}
(1 row)
Run Code Online (Sandbox Code Playgroud)

然后:

=> select y[1].a, y[1].b from footable where x = 1;
 a | b 
---+---
 1 | 2
(1 row)
Run Code Online (Sandbox Code Playgroud)

您还可以在WHERE子句中访问复合类型:

=> select * from footable where y[1].b < 3;
 x |         y         
---+-------------------
 1 | {"(1,2)"}
(1 row)
Run Code Online (Sandbox Code Playgroud)


Szy*_*ski 5

首先,有一个错误,创建表查询中的"as"一词是错误的.你应该写这个:

create table footable (
    x integer,  
    y footype []
);
Run Code Online (Sandbox Code Playgroud)

将数据插入表中:

insert into footable(x, y) values(10, ARRAY[ ROW(1.0,2.0), ROW(3,4)]::footype[] );

# select * from footable;
 x  |         y         
----+-------------------
 10 | {"(1,2)","(3,4)"}
(1 row)
Run Code Online (Sandbox Code Playgroud)

按字段获取数据:

# select x, y[1], y[1].a, y[1].b from footable;

 x  |   y   | a | b 
----+-------+---+---
 10 | (1,2) | 1 | 2
(1 row)
Run Code Online (Sandbox Code Playgroud)