隨筆-102? 文章-0? 評論-83?
# [PostgreSQL的generate_series函數應用](http://www.cnblogs.com/mchina/archive/2013/04/03/2997722.html)
**一、簡介**
PostgreSQL 中有一個很有用處的內置函數generate_series,可以按不同的規則產生一系列的填充數據。
**二、語法**
<table align="left" border="0"><tbody><tr><th width="230" align="left"><span style="font-size: 14px;">函數</span></th> <th width="100" align="left"><span style="font-size: 14px;">參數類型</span></th> <th align="left"><span style="font-size: 14px;">返回類型</span></th> <th align="left"><span style="font-size: 14px;">描述</span></th></tr><tr><td><span style="font-family: courier new,courier; font-size: 12px;">generate_series(start, stop) <br/></span></td> <td><span style="font-family: courier new,courier; font-size: 12px;">int 或 bigint <br/></span></td> <td><span style="font-family: courier new,courier; font-size: 12px;">setof int 或 setof bigint(與參數類型相同) <br/></span></td> <td><span style="font-family: courier new,courier; font-size: 12px;">生成一個數值序列,從start 到 stop,步進為一</span></td></tr><tr><td><span style="font-family: courier new,courier; font-size: 12px;">generate_series(start, stop, step) <br/></span></td> <td><span style="font-family: courier new,courier; font-size: 12px;">int 或 bigint<br/></span></td> <td><span style="font-family: courier new,courier; font-size: 12px;"><span style="font-family: courier new,courier; font-size: 12px;">setof int 或 setof bigint(與參數類型相同) </span></span></td> <td><span style="font-family: courier new,courier; font-size: 12px;"><span style="font-family: courier new,courier; font-size: 12px;">生成一個數值序列,從start 到 stop,步進為step</span></span></td></tr><tr><td><span style="font-family: courier new,courier; font-size: 12px;"><span style="font-family: courier new,courier; font-size: 12px;">generate_series(start, stop, step_interval)</span></span></td> <td><span style="font-family: courier new,courier; font-size: 12px;">timestamp or timestamp with time zone <br/></span></td> <td><span style="font-family: courier new,courier; font-size: 12px;"><span style="font-family: courier new,courier; font-size: 12px;">timestamp 或 timestamp with time zone(same as argument type)</span></span></td> <td><span style="font-family: courier new,courier; font-size: 12px;"><span style="font-family: courier new,courier; font-size: 12px;"><span style="font-family: courier new,courier; font-size: 12px;">生成一個數值序列,從start 到 stop,步進為step</span></span></span></td></tr></tbody></table>
?
**三、實例
**
3.1) int 類型
a. 不寫步進時默認為1
[]( "復制代碼")
~~~
david=# select generate_series(1, 10);
generate_series
-----------------
1
2
3
4
5
6
7
8
9
10
(10 rows)
david=#
~~~
[]( "復制代碼")
b. 設置步進
[]( "復制代碼")
~~~
david=# select generate_series(1, 10, 3);
generate_series
-----------------
1
4
7
10
(4 rows)
david=#
~~~
[]( "復制代碼")
c. 如果step 是正數,而start 大于stop,那么返回零行。相反,如果step 是負數,start 小于stop,則返回零行。如果是NULL 輸入,也產生零行。step 為零則是一個錯誤。
~~~
david=# select generate_series(5,1);
generate_series
-----------------
(0 rows)
david=#
~~~
NULL inputs
~~~
david=# select generate_series(5,null);
generate_series
-----------------
(0 rows)
david=#
~~~
step 為零
~~~
david=# select generate_series(5,1,0);
ERROR: step size cannot equal zero
david=#
~~~
start 大于stop,step 是負數
[]( "復制代碼")
~~~
david=# select generate_series(5,1,-1);
generate_series
-----------------
5
4
3
2
1
(5 rows)
david=#
~~~
[]( "復制代碼")
3.2) 時間類型
[]( "復制代碼")
~~~
david=# select generate_series(now(), now() + '7 days', '1 day');
generate_series
-------------------------------
2013-04-03 14:22:26.391852+08
2013-04-04 14:22:26.391852+08
2013-04-05 14:22:26.391852+08
2013-04-06 14:22:26.391852+08
2013-04-07 14:22:26.391852+08
2013-04-08 14:22:26.391852+08
2013-04-09 14:22:26.391852+08
2013-04-10 14:22:26.391852+08
(8 rows)
david=#
~~~
[]( "復制代碼")
[]( "復制代碼")
~~~
david=# select generate_series(to_date('20130403','yyyymmdd'), to_date('20130404','yyyymmdd'), '3 hours');
generate_series
------------------------
2013-04-03 00:00:00+08
2013-04-03 03:00:00+08
2013-04-03 06:00:00+08
2013-04-03 09:00:00+08
2013-04-03 12:00:00+08
2013-04-03 15:00:00+08
2013-04-03 18:00:00+08
2013-04-03 21:00:00+08
2013-04-04 00:00:00+08
(9 rows)
david=#
~~~
[]( "復制代碼")
3.3) IP類型
a. 建表
~~~
david=# create table tbl_david(id int, ip_start inet, ip_stop inet);
CREATE TABLE
david=#
~~~
b. 插入數據
[]( "復制代碼")
~~~
david=# insert into tbl_david values (1, '192.168.1.6', '192.168.1.10');
INSERT 0 1
david=# insert into tbl_david values (2, '192.168.2.16', '192.168.2.20');
INSERT 0 1
david=# insert into tbl_david values (3, '192.168.3.116', '192.168.3.120');
INSERT 0 1
david=#
~~~
[]( "復制代碼")
c. 查看數據
[]( "復制代碼")
~~~
david=# select * from tbl_david ;
id | ip_start | ip_stop
----+---------------+---------------
1 | 192.168.1.6 | 192.168.1.10
2 | 192.168.2.16 | 192.168.2.20
3 | 192.168.3.116 | 192.168.3.120
(3 rows)
david=#
~~~
[]( "復制代碼")
d. generate_series 生成序列
[]( "復制代碼")
~~~
david=# select id, generate_series(0, ip_stop-ip_start)+ip_start as ip_new from tbl_david ;
id | ip_new
----+---------------
1 | 192.168.1.6
1 | 192.168.1.7
1 | 192.168.1.8
1 | 192.168.1.9
1 | 192.168.1.10
2 | 192.168.2.16
2 | 192.168.2.17
2 | 192.168.2.18
2 | 192.168.2.19
2 | 192.168.2.20
3 | 192.168.3.116
3 | 192.168.3.117
3 | 192.168.3.118
3 | 192.168.3.119
3 | 192.168.3.120
(15 rows)
david=#
~~~
[]( "復制代碼")
**四、總結**
PostgreSQL的generate_series函數對生成測試數據,批量更新一定規則的數據有比較多的應用場景,使用得當可提升開發效率,另外IP的序列生成也是PG的一個亮點。
**五、參考**
- PostgreSQL官方文檔:[http://www.postgresql.org/docs/9.2/static/functions-srf.html](http://www.postgresql.org/docs/9.2/static/functions-srf.html)
- kenyon的個人頁面:[http://my.oschina.net/Kenyon/blog/75099](http://my.oschina.net/Kenyon/blog/75099)
分類: [Postgresql](http://www.cnblogs.com/mchina/category/381458.html)
標簽: [postgresql](http://www.cnblogs.com/mchina/tag/postgresql/), [序列](http://www.cnblogs.com/mchina/tag/%E5%BA%8F%E5%88%97/), [generate_series](http://www.cnblogs.com/mchina/tag/generate_series/), [函數](http://www.cnblogs.com/mchina/tag/%E5%87%BD%E6%95%B0/)
綠色通道: [好文要頂]()[關注我]()[收藏該文]()[與我聯系](http://space.cnblogs.com/msg/send/David_Tang)[]( "分享至新浪微博")
[](http://home.cnblogs.com/u/mchina/)
[David_Tang](http://home.cnblogs.com/u/mchina/)
[關注 - 1](http://home.cnblogs.com/u/mchina/followees)
[粉絲 - 116](http://home.cnblogs.com/u/mchina/followers)
[+加關注]()
1
0
(請您對文章做出評價)
[? ](http://www.cnblogs.com/mchina/archive/2013/03/15/2956017.html) 上一篇:[Linux 配置雙機SSH信任](http://www.cnblogs.com/mchina/archive/2013/03/15/2956017.html "發布于2013-03-15 16:48")
[? ](http://www.cnblogs.com/mchina/archive/2013/04/09/2973427.html) 下一篇:[PostgreSQL分區表(Table Partitioning)應用](http://www.cnblogs.com/mchina/archive/2013/04/09/2973427.html "發布于2013-04-09 11:14")
posted @ 2013-04-03 14:50[David_Tang](http://www.cnblogs.com/mchina/) 閱讀(753) 評論(0) [編輯](http://www.cnblogs.com/mchina/admin/EditPosts.aspx?postid=2997722)[收藏](#)

Copyright ?2013 David_Tang
- 數據表
- 模式Schema
- 表的繼承和分區
- 常用數據類型
- 函數和操作符-一
- 函數和操作符-二
- 函數和操作符-三
- 索引
- 事物隔離
- 性能提升技巧
- 服務器配置
- 角色和權限
- 數據庫管理
- 數據庫維護
- 系統表
- 系統視圖
- SQL語言函數
- PL-pgSQL過程語言
- PostgreSQL 序列(SEQUENCE)
- PostgreSQL的時間-日期函數使用
- PostgreSQL 查看數據庫,索引,表,表空間大小
- 用以查詢某表的詳細 包含表字段的注釋信息
- PostgreSQL 系統表查看系統信息
- postgre存儲過程簡單實用方法
- PostgreSQL實用日常維護SQL
- PostgreSQL的時間函數使用整理
- 命令
- pg_ctl控制服務器
- initdb 初始化數據庫簇
- createdb創建數據庫
- dropdb 刪除數據庫
- createuser創建用戶
- dropuser 刪除用戶
- psql交互式工具
- psql命令手冊
- pg_dump 數據庫轉儲
- pg_restore恢復數據庫
- vacuumdb 清理優化數據庫
- reindexdb 數據庫重創索引
- createlang 安裝過程語言
- droplang 刪除過程語言
- pg_upgrade 升級數據庫簇
- 調試存儲過程
- 客戶端命令-一
- 客戶端命令-二
- 使用技巧
- PostgreSQL刪除重復數據
- postgresql 小技巧
- PostgreSQL的10進制與16進制互轉
- PostgreSQL的漢字轉拼音
- Postgres重復數據的更新一例
- PostgreSQL使用with一例
- PostgreSQL在函數內返回returning
- PostgreSQL中的group_concat使用
- PostgreSQL數據庫切割和組合字段函數
- postgresql重復數據的刪除
- PostgreSQL的遞歸查詢(with recursive)
- PostgreSQL函數如何返回數據集
- PostgreSQL分區表(Table Partitioning)應用 - David_Tang - 博客園
- PostgreSQL: function 返回結果集多列和單列的例子
- 利用pgAgent創建定時任務
- 淺談 PostgreSQL 類型轉換類似Oracle
- postgresql在windows(包括win7)下的安裝配置
- PostgreSQL簡介、安裝、用戶管理、啟動關閉、創建刪除數據庫 (2010-11-08 12-52-51)轉載▼標簽: 雜談分類: PostgreSQL
- PostgreSQL的generate_series函數應用
- PostgreSQL 8.3.1 全文檢索(Full Text Search)
- postgresql record 使用
- 備份恢復
- PostgreSQL基于時間點恢復(PITR)
- Postgresql基于時間點恢復PITR案例(二)
- Postgres邏輯備份腳本
- Postgres invalid command \N數據恢復處理