• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            colorful

            zc qq:1337220912

             

            PostgreSQL: 數(shù)組類型(array) 的使用

            http://francs3.blog.163.com/blog/static/405767272011103105752290/
              PostgreSQL 支持?jǐn)?shù)組類型,包括一維數(shù)組和多維數(shù)組,在某些應(yīng)用場合數(shù)組的應(yīng)用還是很需要的,
            這里簡單介紹下一維數(shù)組的使用及有關(guān)數(shù)組函數(shù)和操作符的使用。
              
              
            --定義數(shù)組
            mydb=> create table test_array(id serial primary key, phone int8[]);
            NOTICE:  CREATE TABLE will create implicit sequence "test_array_id_seq" for serial column "test_array.id"
            NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "test_array_pkey" for table "test_array"
            CREATE TABLE

            mydb=> \d test_array
                                       Table "mydb.test_array"
             Column |   Type   |                        Modifiers                       
            --------+----------+---------------------------------------------------------
             id     | integer  | not null default nextval('test_array_id_seq'::regclass)
             phone  | bigint[] |
            Indexes:
                "test_array_pkey" PRIMARY KEY, btree (id)


            --數(shù)組元素插入有兩種方式
            mydb=> insert into test_array(phone) values ('{1,2}');
            INSERT 0 1
            mydb=> insert into test_array(phone) values ('{2,3}');
            INSERT 0 1

            mydb=> insert into test_array(phone) values (array[3,4,5]);
            INSERT 0 1

            mydb=> select * From test_array;
             id |  phone 
            ----+---------
              1 | {1,2}
              2 | {2,3}
              3 | {3,4,5}
            (3 rows)


            --數(shù)組元素的引用
            mydb=> select phone  from test_array where id=1;
             phone
            -------
             {1,2}
            (1 row)

            mydb=> select phone[1],phone[2]  from test_array where id=1;
             phone | phone
            -------+-------
                 1 |     2
                
                
                
            一 常見的數(shù)組操作(Array Operators)

            PostgreSQL: 數(shù)組類型(array) 的使用 - francs - My DBA LIFE

             

            --equal
            mydb=>  select array[1,2]=array[1.1,2.1]::int[];
             ?column?
            ----------
             t
            (1 row)

            --not equal
            mydb=> select array[1,2] <> array[1,2,3];
             ?column?
            ----------
             t
            (1 row)


            --less than
            mydb=> select ARRAY[1,2,3] < ARRAY[1,2,4];
             ?column?
            ----------
             t
            (1 row)


            --greater than
            mydb=> select ARRAY[1,4,3] > ARRAY[1,2,4];
             ?column?
            ----------
             t
            (1 row)


            --contains
            mydb=> select ARRAY[1,4,3] @> ARRAY[3,1];
             ?column?
            ----------
             t
            (1 row)


            --is contained by
            mydb=> select ARRAY[2,7] <@ ARRAY[1,7,4,2,6];
             ?column?
            ----------
             t
            (1 row)


            --overlap (have elements in common)
            mydb=> select ARRAY[1,4,3] && ARRAY[2,1];
             ?column?
            ----------
             t
                

            二 常見數(shù)組函數(shù)( Array Functions )
            --將數(shù)據(jù)元素追加到數(shù)組
            mydb=> select array_append(array[2,3,4],5);
             array_append
            --------------
             {2,3,4,5}
            (1 row)

            --連接兩個(gè)數(shù)組
            mydb=> select array_cat(array[1,2],array[3,4]);
             array_cat
            -----------
             {1,2,3,4}
            (1 row)

            --獲得數(shù)組的維度
            mydb=> select array_ndims(array[1,2,3]);
             array_ndims
            -------------
                       1
            (1 row)

            mydb=> select array_ndims(array[[1,2,3],[4,5,6]]);
             array_ndims
            -------------
                       2
            (1 row)


            --獲得數(shù)組的長度                                 ^
            mydb=> select array_length(array[1,2,3],1);
             array_length
            --------------
                        3
            (1 row)

            mydb=> select array_length(array[[1,2],[2,3]],1);
             array_length
            --------------
                        2
            (1 row)


            三 intarray 模塊的數(shù)組函數(shù)
            --獲取元素個(gè)數(shù)據(jù)總和
            mydb=> select icount(array[1,2]);
             icount
            --------
                  2
            (1 row)

            mydb=> select icount(array[[1,2],[2,3]]);
             icount
            --------
                  4
            (1 row)


            --排序
            mydb=> select sort_asc(array[4,8,7]);
             sort_asc
            ----------
             {4,7,8}
            (1 row)

            mydb=> select sort_desc(array[4,8,7]);
             sort_desc
            -----------
             {8,7,4}
            (1 row)

            mydb=> select sort_desc(array[[4,8,7],[8,9,7]]);
                 sort_desc    
            -------------------
             {{9,8,8},{7,7,4}}
            (1 row)


            四 intarray 模塊的數(shù)組操作符

            PostgreSQL: 數(shù)組類型(array) 的使用 - francs - My DBA LIFE

             

            --表數(shù)據(jù)
            mydb=> select * from test_array;
             id |  phone 
            ----+---------
              1 | {1,2}
              2 | {2,3}
              3 | {3,4,5}
              4 | {4,5,6}
              5 | {4,5,7}
            (5 rows)


            --查找包括相同元素的記錄
            mydb=> select id ,phone from test_array where phone && array[1,2]::int8[];
             id | phone
            ----+-------
              1 | {1,2}
              2 | {2,3}
            (2 rows)


            --查找數(shù)組元素的交集
            mydb=> select array[1,2,3] & array[3,4,5];
             ?column?
            ----------
             {3}
            (1 row)


            五 索引的使用
              
                      數(shù)組支持創(chuàng)建 GiST 和 GIN 類型索引,這兩類索引的選擇要根據(jù)場合,簡單的說, GIN 類型索引在查詢上要比
              GiST 類型索引快,但在 update 的時(shí)候要慢些,所以 GIN 類型索引適合表數(shù)據(jù)不太變化的場合,而 GiST 索引適用
              于表數(shù)據(jù)經(jīng)常需要 UPDATE 的場景。

            posted on 2012-06-08 16:04 多彩人生 閱讀(1469) 評論(0)  編輯 收藏 引用 所屬分類: postgresql

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(3)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            99久久人人爽亚洲精品美女| 久久久亚洲裙底偷窥综合| 国产99精品久久| 久久黄视频| 色妞色综合久久夜夜| 久久国产乱子精品免费女| 久久男人AV资源网站| 精品国产乱码久久久久软件| 精品久久久久久久无码| 一本久道久久综合狠狠躁AV| 国产韩国精品一区二区三区久久| 久久天天躁狠狠躁夜夜2020老熟妇| 国产成人精品久久| 久久青青草原精品国产不卡| 777米奇久久最新地址| 少妇熟女久久综合网色欲| 国产福利电影一区二区三区,免费久久久久久久精 | 国产精品免费福利久久| 久久久中文字幕日本| AV无码久久久久不卡蜜桃| 2021国产精品午夜久久| 久久99国产精品成人欧美| 久久国产精品-国产精品| 亚洲AV无码久久精品成人 | 国产99久久久国产精品小说| 色偷偷888欧美精品久久久| 久久精品国产亚洲av麻豆色欲| 亚洲国产天堂久久久久久| 91久久国产视频| 久久精品国产一区| 久久无码av三级| 99久久免费国产精精品| 国产精品美女久久久久久2018| 欧美牲交A欧牲交aⅴ久久| 亚洲中文字幕无码久久综合网| 中文字幕亚洲综合久久菠萝蜜| 色偷偷88欧美精品久久久| 一级女性全黄久久生活片免费| 欧美国产精品久久高清| 成人综合久久精品色婷婷 | 国产精品美女久久久久网|