加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

将postgresql数组解包成行

发布时间:2020-12-13 16:40:58 所属栏目:百科 来源:网络整理
导读:在PostgreSQL中将数组解包成行的最快方法是什么?即 我们有: a-{1,2}{2,3,4} 我们需要: b- 12234 我使用: select explode_array(a) as a from a_table; 其中explode_array是: create or replace function explode_array(in_array anyarray) returns seto
在PostgreSQL中将数组解包成行的最快方法是什么?即

我们有:

a
-
{1,2}
{2,3,4}

我们需要:

b
- 
1
2
2
3
4

我使用:

select explode_array(a) as a from a_table;

其中explode_array是:

create or replace function explode_array(in_array anyarray) returns setof anyelement as
$$
    select ($1)[s] from generate_series(1,array_upper($1,1)) as s;
$$

有什么更好的办法吗?

使用 unnest.例如:
CREATE OR REPLACE FUNCTION test( p_test text[] )
  RETURNS void AS
$BODY$
BEGIN
  SELECT id FROM unnest( p_test ) AS id;
END;
$BODY$
  LANGUAGE plpgsql IMMUTABLE
  COST 1;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读