PostgreSQL 选择复合元素数组

发布于 2024-11-26 15:18:13 字数 240 浏览 0 评论 0原文

我有一个通过复合类型定义的表:

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

create table footable as (  
x integer,  
y footype []);

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

预先感谢,
安东尼奥

I've got a table defined over a composite type:

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

create table footable as (  
x integer,  
y footype []);

How can I use a select statement on the single field of the composite elements contained in the table?

Thanks in advance,
Antonio

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

相思碎 2024-12-03 15:18:13

只是通常的 数组访问语法 后跟通常的 < a href="http://www.postgresql.org/docs/current/static/rowtypes.html#AEN7383" rel="noreferrer">复合类型访问语法。为了演示目的,进行一些设置:

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

然后:

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

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

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

Just the usual array access syntax followed by the usual composite type access syntax. So a bit of setup for demonstration purposes:

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

And then:

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

You can also access the composite type inside a WHERE clause:

=> select * from footable where y[1].b < 3;
 x |         y         
---+-------------------
 1 | {"(1,2)"}
(1 row)
浪漫之都 2024-12-03 15:18:13

首先,有一个错误,建表查询中的“as”一词是错误的。你应该这样写:

create table footable (
    x integer,  
    y footype []
);

向表中插入数据:

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)

通过字段获取数据:

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

 x  |   y   | a | b 
----+-------+---+---
 10 | (1,2) | 1 | 2
(1 row)

First of all, there is one mistake, the word 'as' is wrong in the create table query. You should rather write this:

create table footable (
    x integer,  
    y footype []
);

Insert data into the table:

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)

Get the data by the fields:

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

 x  |   y   | a | b 
----+-------+---+---
 10 | (1,2) | 1 | 2
(1 row)
不语却知心 2024-12-03 15:18:13

我已经纠正了不起作用的解决方案,因为它在generate_subscripts()函数中存在错误。我更改了此函数中的列,因为它必须是数组类型列 (y),而不是原子类型列 (x):

=> select * from (select x, y, generate_subscripts(y,1) as s from footable) as coord where y[s].a = 2.5;

I have corrected the solution that didn't work because it had a mistake in the generate_subscripts() function. I have changed the column in this function because it must be an array type column (y), not an atomic one (x):

=> select * from (select x, y, generate_subscripts(y,1) as s from footable) as coord where y[s].a = 2.5;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文