• <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: 數組類型(array) 的使用

            http://francs3.blog.163.com/blog/static/405767272011103105752290/
              PostgreSQL 支持數組類型,包括一維數組和多維數組,在某些應用場合數組的應用還是很需要的,
            這里簡單介紹下一維數組的使用及有關數組函數和操作符的使用。
              
              
            --定義數組
            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)


            --數組元素插入有兩種方式
            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)


            --數組元素的引用
            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
                
                
                
            一 常見的數組操作(Array Operators)

            PostgreSQL: 數組類型(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
                

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

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

            --獲得數組的維度
            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)


            --獲得數組的長度                                 ^
            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 模塊的數組函數
            --獲取元素個數據總和
            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 模塊的數組操作符

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

             

            --表數據
            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)


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


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

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

            導航

            統計

            常用鏈接

            留言簿(3)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久婷婷国产综合精品| 久久人人爽人人爽人人av东京热| 亚洲精品午夜国产va久久| 少妇被又大又粗又爽毛片久久黑人| 国内精品久久久久久久久电影网| 久久亚洲国产成人影院网站| 久久婷婷国产综合精品| 香蕉久久夜色精品国产小说| 久久久久99这里有精品10 | 热99RE久久精品这里都是精品免费 | 一级做a爰片久久毛片毛片| 久久99国产综合精品女同| 久久精品人妻一区二区三区| 奇米影视7777久久精品人人爽 | 99久久精品午夜一区二区| 亚洲国产成人乱码精品女人久久久不卡 | 久久天天躁狠狠躁夜夜躁2O2O| 国产A三级久久精品| 久久97久久97精品免视看| 久久狠狠爱亚洲综合影院| 国产高清国内精品福利99久久| 久久久久人妻一区精品性色av| 久久久久亚洲精品日久生情| 精品久久久久久中文字幕| 97久久精品人妻人人搡人人玩 | 久久久久无码精品| 精品一区二区久久| 久久精品人成免费| 亚洲国产精品无码久久SM| 欧美喷潮久久久XXXXx| 伊人久久大香线蕉综合5g| 久久久久亚洲精品无码网址| 久久99免费视频| 狠狠色噜噜狠狠狠狠狠色综合久久| 色婷婷久久综合中文久久蜜桃av| 亚洲国产成人乱码精品女人久久久不卡 | 一级做a爰片久久毛片免费陪| 久久精品天天中文字幕人妻| 久久精品国产亚洲AV无码偷窥 | 亚洲国产成人久久综合一 | 久久AV无码精品人妻糸列|