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)文章
使用PostgreSQL為表或視圖創(chuàng)建備注的操作
這篇文章主要介紹了使用PostgreSQL為表或視圖創(chuàng)建備注的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
關(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主從復(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 刪除重復(fù)數(shù)據(jù)案例詳解
這篇文章主要介紹了postgresql 刪除重復(fù)數(shù)據(jù)案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
PostgreSQL基礎(chǔ)知識之SQL操作符實踐指南
這篇文章主要給大家介紹了關(guān)于PostgreSQL基礎(chǔ)知識之SQL操作符實踐的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用PostgreSQL具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05

