PostgreSQL--數(shù)組類型操作

一、數(shù)組類型創(chuàng)建表

數(shù)組類型,要求數(shù)組內(nèi)的元素屬于同一種類型,當(dāng)出現(xiàn)No function matches the given name and argument types. You might need to add explicit type casts.報錯的時候,說明 list 的格式和插入數(shù)據(jù)或者修改數(shù)據(jù)的格式不同導(dǎo)致的, 類型很重要,需要保證類型相同才可以操作
 

長安網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)公司,長安網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為長安上千提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢,請找那個售后服務(wù)好的長安做網(wǎng)站的公司定做!

1.1、建表指定數(shù)組類型

只需要在表字段類型后面加'[]'

postgres=# create table test1 (
    id serial, 
    arr int[], 
    name varchar(10)[], 
    age char(10)[], 
    score float[]
    ); 

postgres=# \d+ test1;
                                                 Table "public.test1"
     Column |          Type           |                     Modifiers                      | Storage  | Stats target | Description 
--------+-------------------------+----------------------------------------------------+----------+--------------+-------------
 id     | integer                 | not null default nextval('test1_id_seq'::regclass) | plain    |              | 
 arr    | integer[]               |                                                    | extended |              | 
 name   | character varying(10)[] |                                                    | extended |              | 
 age    | character(10)[]         |                                                    | extended |              | 
 score  | double precision[]      |                                                    | extended |              | 

 

1.2、數(shù)據(jù)插入

postgres=# insert into test(id, uid) values(3, '{1, 2, 3}');        插入數(shù)組方式1
postgres=# insert into test(id, uid) values(3, array[20, 30]::int8[]);   插入數(shù)組方式二

1.3、修改數(shù)組:

postgres=# update test set uid = uid || '{0}';    后面追加一個數(shù)組
postgres=# update test set uid='{0,0}' || uid;   在前面插入一個數(shù)組
postgres=# update arr_test set uid=array_append(uid, '1'::int);   指明類型追加一個數(shù)
postgres=# update arr_test set uid=array_append(uid, 1);   按默認(rèn)int類型追加一個數(shù)
postgres=# update arr_test set uid=array_prepend('1'::int, uid);     在前面插入一個數(shù)

1.4、刪除數(shù)組中的數(shù)據(jù)

postgres=# update arr_test set uid=array_remove(uid, '1'::int);  指明類型移除指定的數(shù)

1.5、查找數(shù)組中的數(shù)據(jù)

postgres=# select * from test where 20=any(uid);    #uid數(shù)組中存在20的row
postgres=# select * from test where uid && array[20, 1]::int8[];   uid數(shù)組中和array[20, 1]存在交集的
postgres=# select * from arr_test where uid@>'{1, 2}';   uid 數(shù)組中同時包含[1, 2]的
postgres=# select * from arr_test where uid<@'{1, 2}';   uid 數(shù)組被[1, 2]包含的

postgres=# select * from arr_test where 2=uid[1]; 使用uid 數(shù)組下標(biāo)查詢,下標(biāo)是從1開始的
postgres=# select id, uid[2] from arr_test; 使用下標(biāo)顯示

網(wǎng)頁題目:PostgreSQL--數(shù)組類型操作
瀏覽路徑:http://muchs.cn/article26/jpepcg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、搜索引擎優(yōu)化Google、定制開發(fā)定制網(wǎng)站、外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)