gpt4 book ai didi

sql - 将多维数组转换为记录

转载 作者:行者123 更新时间:2023-11-29 11:36:17 26 4
gpt4 key购买 nike

给定:{{1,"a"},{2,"b"},{3,"c"}}
期望:

 foo | bar
-----+------
1 | a
2 | b
3 | c

您可以通过以下查询获得预期的结果;但是,最好有一些随数组大小而缩放的东西。

SELECT arr[subscript][1] as foo, arr[subscript][2] as bar
FROM ( select generate_subscripts(arr,1) as subscript, arr
from (select '{{1,"a"},{2,"b"},{3,"c"}}'::text[][] as arr) input
) sub;

最佳答案

这个有效:

select key as foo, value as bar
from json_each_text(
json_object('{{1,"a"},{2,"b"},{3,"c"}}')
);

结果:

 foo | bar
-----+------
1 | a
2 | b
3 | c

Docs

关于sql - 将多维数组转换为记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18111008/

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