最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PostgreSQL 對數(shù)組的遍歷操作

 更新時間:2021年01月28日 09:04:57   作者:懶得去死  
這篇文章主要介紹了PostgreSQL 對數(shù)組的遍歷操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

PostgreSQL 提供了數(shù)組類型。

我來演示下如何具體使用

創(chuàng)建一個有數(shù)組類型字段的表。

create table test_array(id serial primary key, str1 int[][][]);

插入兩條測試數(shù)據(jù)。

insert into test_array values (1,array[[[1,2],[3,4],[5,6]],[[20,30],[40,50],[70,100]]]);
insert into test_array values (2,array[[[100,200],[300,400],[500,600]],[[2000,3000],[4000,5000],[7000,10000]]]);

為了能直觀的看到結(jié)果集,我們得把數(shù)組的值換成普通的類型拿出來, 有以下幾種方法。

不帶分片的遍歷,

create or replace function sp_array2table_simple(
anyarray
)
returns table (element int) as
$ytt$
declare array1 alias for $1;
 x int;
begin
 drop table if exists tmp_1;
 create temporary table tmp_1 (id int);
 
 <<label1>> foreach x in array array1
 loop
 insert into tmp_1 values (x);
 end loop label1;
 
 return query select * from tmp_1;
end;
$ytt$ language plpgsql; 
 
t_girl=#select sp_array2table_simple(str1) as array_list from test_array where id = 2; 
 
 array_list
------------
  100
  200
  300
  400
  500
  600
  2000
  3000
  4000
  5000
  7000
  10000
(12 行記錄) 
 
時間:7.780 ms

帶分片的遍歷:

create or replace function sp_array2table(
anyarray
)
returns table (element int) as
$ytt$
declare array1 alias for $1;
 x int[];
 nlen int := 0;
 i int := 1; 
begin
 drop table if exists tmp_1;
 create temporary table tmp_1 (id int);
 
 <<label1>> foreach x slice 1 in array array1
 loop
  nlen := array_length(x,1);
  i := 1;
  <<label2>> while i <= nlen loop
  insert into tmp_1 values (x[i]);
  i := i + 1;
  end loop label2;
 end loop label1;
 
 return query select * from tmp_1;
end;
$ytt$ language plpgsql; 
 
t_girl=#select sp_array2table(str1) as array_list from test_array where id = 2; 
 
 array_list
------------
  100
  200
  300
  400
  500
  600
  2000
  3000
  4000
  5000
  7000
  10000
(12 行記錄) 
 
時間:20.139 ms

還有就是系統(tǒng)系統(tǒng)了幾個函數(shù),直接進(jìn)行遍歷,

比如unnest

t_girl=#select unnest(str1) as array_list from test_array where id = 2; 
 
 array_list
------------
  100
  200
  300
  400
  500
  600
  2000
  3000
  4000
  5000
  7000
  10000
(12 行記錄) 
 
時間:1.002 ms

比如array_to_string 等。

t_girl=#select regexp_split_to_table(array_to_string(str1,','),',+') as array_list from test_array where id = 2;
 
 array_list
------------
 100
 200
 300
 400
 500
 600
 2000
 3000
 4000
 5000
 7000
 10000
(12 行記錄) 
 
時間:0.850 ms

補(bǔ)充:PostgreSQL遍歷Json

SQL:

SELECT
 orderno,
 fromno,
 fromamount,
 fromlotno ->> 'index' fromlotno,
 othercondition ->> 'supplicode' supplicode,
 othercondition ->> 'downcode' downcode,
 othercondition ->> 'spec' spec,
 othercondition ->> 'carport' carport
FROM
 (
 SELECT
 orderno,
 fromno,
 fromamount,
 json_array_elements (fromlotno) fromlotno,
 json_array_elements (othercondition) othercondition
 FROM
 t_feather_source
 ) A

輸出結(jié)果:

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

  • postgres 數(shù)據(jù)庫遷移的幾種方法

    postgres 數(shù)據(jù)庫遷移的幾種方法

    本文詳細(xì)介紹了postgres 數(shù)據(jù)庫遷移的幾種方法,包括數(shù)據(jù)文件備份、數(shù)據(jù)文件遷移方案以及常見問題及其解決方案,感興趣的可以了解一下
    2025-07-07
  • 使用PostgreSQL為表或視圖創(chuàng)建備注的操作

    使用PostgreSQL為表或視圖創(chuàng)建備注的操作

    這篇文章主要介紹了使用PostgreSQL為表或視圖創(chuàng)建備注的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • 關(guān)于PostgreSql數(shù)據(jù)庫與mysql數(shù)據(jù)庫的不同點以及注意事項

    關(guān)于PostgreSql數(shù)據(jù)庫與mysql數(shù)據(jù)庫的不同點以及注意事項

    PostgreSQL和MySQL是兩種流行的關(guān)系型數(shù)據(jù)庫管理系統(tǒng)(RDBMS),它們都可以用來存儲和管理數(shù)據(jù),但是它們在某些方面有所不同,下面這篇文章主要給大家介紹了關(guān)于PostgreSql數(shù)據(jù)庫與mysql數(shù)據(jù)庫的不同點以及注意事項的相關(guān)資料,需要的朋友可以參考下
    2023-05-05
  • PostgreSQL 更新視圖腳本的注意事項說明

    PostgreSQL 更新視圖腳本的注意事項說明

    這篇文章主要介紹了PostgreSQL 更新視圖腳本的注意事項說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • Postgresql主從異步流復(fù)制方案的深入探究

    Postgresql主從異步流復(fù)制方案的深入探究

    這篇文章主要給大家介紹了關(guān)于Postgresql主從異步流復(fù)制方案的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Postgresql具有一起的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-10-10
  • PostgreSQL主從復(fù)制實戰(zhàn)指南:告別單點故障,附主從切換與延遲監(jiān)控命令

    PostgreSQL主從復(fù)制實戰(zhàn)指南:告別單點故障,附主從切換與延遲監(jiān)控命令

    文章詳細(xì)介紹了如何在CentOS/Ubuntu環(huán)境下搭建PostgreSQL主從復(fù)制,包括環(huán)境準(zhǔn)備、主庫配置、從庫配置、驗證復(fù)制狀態(tài)等步驟,通過主從復(fù)制,可以實現(xiàn)數(shù)據(jù)庫高可用,減少宕機(jī)時間,提高系統(tǒng)容災(zāi)能力,感興趣的朋友一起看看吧
    2026-04-04
  • PostgreSQL拆分字符串的三種方式

    PostgreSQL拆分字符串的三種方式

    這篇文章給大家介紹了PostgreSQL拆分字符串的三種方式,字符串轉(zhuǎn)為數(shù)組,字符串轉(zhuǎn)為列表和字符串轉(zhuǎn)為數(shù)據(jù)項,并通過代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2024-01-01
  • postgresql之greenplum字符串去重拼接方式

    postgresql之greenplum字符串去重拼接方式

    這篇文章主要介紹了postgresql之greenplum字符串去重拼接方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • postgresql 刪除重復(fù)數(shù)據(jù)案例詳解

    postgresql 刪除重復(fù)數(shù)據(jù)案例詳解

    這篇文章主要介紹了postgresql 刪除重復(fù)數(shù)據(jù)案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • PostgreSQL基礎(chǔ)知識之SQL操作符實踐指南

    PostgreSQL基礎(chǔ)知識之SQL操作符實踐指南

    這篇文章主要給大家介紹了關(guān)于PostgreSQL基礎(chǔ)知識之SQL操作符實踐的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用PostgreSQL具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05

最新評論

谢通门县| 确山县| 阳东县| 涞源县| 遂昌县| 若羌县| 杭锦后旗| 栖霞市| 贡嘎县| 永平县| 平昌县| 松滋市| 太原市| 北票市| 满城县| 乌拉特后旗| 五指山市| 新丰县| 保康县| 南乐县| 乳山市| 庆城县| 资源县| 镇康县| 德昌县| 沁水县| 兴化市| 温宿县| 康乐县| 确山县| 都江堰市| 洛浦县| 永春县| 定州市| 梅河口市| 闸北区| 海宁市| 云阳县| 余姚市| 楚雄市| 广灵县|