Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4154316
  • 博文数量: 240
  • 博客积分: 11504
  • 博客等级: 上将
  • 技术积分: 4277
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-28 14:24
文章分类

全部博文(240)

分类: Mysql/postgreSQL

2014-01-24 15:37:22

PostgreSQL 提供了数组类型。我来演示下如何具体使用。


创建一个有数组类型字段的表。


点击(此处)折叠或打开

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


  2. 插入两条测试数据。
  3. insert into test_array values (1,array[[[1,2],[3,4],[5,6]],[[20,30],[40,50],[70,100]]]);
  4. insert into test_array values (2,array[[[100,200],[300,400],[500,600]],[[2000,3000],[4000,5000],[7000,10000]]]);






为了能直观的看到结果集,我们得把数组的值换成普通的类型拿出来, 有以下几种方法。


不带分片的遍历,


点击(此处)折叠或打开

  1. create or replace function sp_array2table_simple(
  2. anyarray
  3. )
  4. returns table (element int) as
  5. $ytt$
  6. declare array1 alias for $1;
  7. x int;
  8. begin
  9.   drop table if exists tmp_1;
  10.   create temporary table tmp_1 (id int);
  11.   
  12.   <<label1>> foreach x in array array1
  13.   loop
  14.     insert into tmp_1 values (x);
  15.   end loop label1;
  16.   
  17.   return query select * from tmp_1;
  18. end;
  19. $ytt$ language plpgsql;


  20. t_girl=#select sp_array2table_simple(str1) as array_list from test_array where id = 2;


  21.  array_list
  22. ------------
  23.         100
  24.         200
  25.         300
  26.         400
  27.         500
  28.         600
  29.        2000
  30.        3000
  31.        4000
  32.        5000
  33.        7000
  34.       10000
  35. (12 行记录)




  36. 时间:7.780 ms




带分片的遍历:


点击(此处)折叠或打开

  1. create or replace function sp_array2table(
  2. anyarray
  3. )
  4. returns table (element int) as
  5. $ytt$
  6. declare array1 alias for $1;
  7. x int[];
  8. nlen int := 0;
  9. i int := 1;
  10. begin
  11.   drop table if exists tmp_1;
  12.   create temporary table tmp_1 (id int);
  13.   
  14.   <<label1>> foreach x slice 1 in array array1
  15.     loop
  16.       nlen := array_length(x,1);
  17.       i := 1;
  18.       <<label2>> while i <= nlen loop
  19.         insert into tmp_1 values (x[i]);
  20.         i := i + 1;
  21.       end loop label2;
  22.   end loop label1;
  23.   
  24.   return query select * from tmp_1;
  25. end;
  26. $ytt$ language plpgsql;


  27. t_girl=#select sp_array2table(str1) as array_list from test_array where id = 2;


  28.  array_list
  29. ------------
  30.         100
  31.         200
  32.         300
  33.         400
  34.         500
  35.         600
  36.        2000
  37.        3000
  38.        4000
  39.        5000
  40.        7000
  41.       10000
  42. (12 行记录)




  43. 时间:20.139 ms




还有就是系统系统了几个函数,直接进行遍历,
比如unnest



点击(此处)折叠或打开

  1. t_girl=#select unnest(str1) as array_list from test_array where id = 2;


  2.  array_list
  3. ------------
  4.         100
  5.         200
  6.         300
  7.         400
  8.         500
  9.         600
  10.        2000
  11.        3000
  12.        4000
  13.        5000
  14.        7000
  15.       10000
  16. (12 行记录)




  17. 时间:1.002 ms




比如array_to_string 等。



点击(此处)折叠或打开

  1. t_girl=#select regexp_split_to_table(array_to_string(str1,','),',+') as array_list from test_array where id = 2;


  2.  array_list
  3. ------------
  4.  100
  5.  200
  6.  300
  7.  400
  8.  500
  9.  600
  10.  2000
  11.  3000
  12.  4000
  13.  5000
  14.  7000
  15.  10000
  16. (12 行记录)




  17. 时间:0.850 ms


阅读(6183) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~