gpt4 book ai didi

postgresql - 在 Postgres 中将复合数组 UNNEST 成行和列

转载 作者:行者123 更新时间:2023-12-05 00:46:15 25 4
gpt4 key购买 nike

Postgres 11.7.

我正在尝试解压缩一个数组,其中每个项目都有多个元素,并且似乎无法正确使用语法。我希望有人能指出我所缺少的。这是一个例子:

select
unnest(array[

('Red Large Special', 1),
('Blue Small', 5),
('Green Medium Special', 87)

]) as item_list

这就是我想要的:

item_name               item_id  
Red Large Special 1
Blue Small 5
Green Medium Special 87

这是我得到的:

base_strings
("Red Large Special",1)
("Blue Small",5)
("Green Medium Special",87)

我认为我需要一个列规范列表,如下所示:

select * from
unnest(array[

('Red Large Special', 1),
('Blue Small', 5),
('Green Medium Special', 87)

]) AS item_list(item_name citext, item_id int4)

我得到的是:

ERROR:  function return row and query-specified return row do not match
DETAIL: Returned type unknown at ordinal position 1, but query expects citext. (Line 9)

如果我正式声明一个自定义的复合类型,我可以让它工作:

CREATE TYPE item_details AS (
item_name citext,
item_id int4);

select * from
unnest(array[

('Red Large Special', 1),
('Blue Small', 5),
('Green Medium Special', 87)

]::item_details[]) as item_list

这是对的:

item_name             item_id
Red Large Special 1
Blue Small 5
Green Medium Special 87

有没有办法在不声明类型的情况下获得相同的结果?我正在寻找一种可以即时定义类型的解决方案。我很确定我过去在 Postgres 中做过这个,但也许是用 JSONB?

我已经查阅了有关表返回表达式的精细文档,但无法遵循它。那里没有真正的例子,我无法从语法摘要中推断出来。

https://www.postgresql.org/docs/current/queries-table-expressions.html

跟进

阻止我追逐自己的尾部的两个很好的答案。在这种情况下,任务是向多个客户端开放一些功能,所以我可能最好使用 JSON 而不是 Postgres 特定的数组语法。 @a_horse_with_no_name 引导我找到这种代码,从 JSON 文本开始:

with expanded_data AS (
select *
from json_to_recordset(
'[
{"base_text":"Red Large Special","base_id":1},
{"base_text":"Blue Small","base_id":5},
{"base_text":"Green Medium Special","base_id":87}
]')
AS unpacked (base_text citext, base_id citext)
)

select base_text,
base_id

from expanded_data

最佳答案

我能想到的一种方法,就是把它转成jsonb数组:

select item ->> 'f1' as item_name, 
(item ->> 'f2')::int as item_id
from jsonb_array_elements(to_jsonb(array[
('Red Large Special', 1),
('Blue Small', 5),
('Green Medium Special', 87)
])) t(item)

关于postgresql - 在 Postgres 中将复合数组 UNNEST 成行和列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61678925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com