企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
随笔-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 [![](https://box.kancloud.cn/2015-10-30_5632e1cc1b197.gif)]( "复制代码") ~~~ david=# select generate_series(1, 10); generate_series ----------------- 1 2 3 4 5 6 7 8 9 10 (10 rows) david=# ~~~ [![](https://box.kancloud.cn/2015-10-30_5632e1cc2bc16.gif)]( "复制代码") b. 设置步进 [![](https://box.kancloud.cn/2015-10-30_5632e1cc36da0.gif)]( "复制代码") ~~~ david=# select generate_series(1, 10, 3); generate_series ----------------- 1 4 7 10 (4 rows) david=# ~~~ [![](https://box.kancloud.cn/2015-10-30_5632e1cc45288.gif)]( "复制代码") 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 是负数 [![](https://box.kancloud.cn/2015-10-30_5632e1cc50f7b.gif)]( "复制代码") ~~~ david=# select generate_series(5,1,-1); generate_series ----------------- 5 4 3 2 1 (5 rows) david=# ~~~ [![](https://box.kancloud.cn/2015-10-30_5632e1cc63e29.gif)]( "复制代码") 3.2) 时间类型 [![](https://box.kancloud.cn/2015-10-30_5632e1cc725fb.gif)]( "复制代码") ~~~ 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=# ~~~ [![](https://box.kancloud.cn/2015-10-30_5632e1cc80527.gif)]( "复制代码") [![](https://box.kancloud.cn/2015-10-30_5632e1cc8fe74.gif)]( "复制代码") ~~~ 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=# ~~~ [![](https://box.kancloud.cn/2015-10-30_5632e1cc9eb0b.gif)]( "复制代码") 3.3) IP类型 a. 建表 ~~~ david=# create table tbl_david(id int, ip_start inet, ip_stop inet); CREATE TABLE david=# ~~~ b. 插入数据 [![](https://box.kancloud.cn/2015-10-30_5632e1ccad229.gif)]( "复制代码") ~~~ 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=# ~~~ [![](https://box.kancloud.cn/2015-10-30_5632e1ccb99d8.gif)]( "复制代码") c. 查看数据 [![](https://box.kancloud.cn/2015-10-30_5632e1ccc6f85.gif)]( "复制代码") ~~~ 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=# ~~~ [![](https://box.kancloud.cn/2015-10-30_5632e1ccd5a0a.gif)]( "复制代码") d. generate_series 生成序列 [![](https://box.kancloud.cn/2015-10-30_5632e1cce2830.gif)]( "复制代码") ~~~ 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=# ~~~ [![](https://box.kancloud.cn/2015-10-30_5632e1ccee057.gif)]( "复制代码") **四、总结** 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)[![](https://box.kancloud.cn/2015-10-30_5632e1cd04e5c.png)]( "分享至新浪微博") [![](https://box.kancloud.cn/2015-10-30_5632e1cd12de9.jpg)](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)[收藏](#) ![](https://box.kancloud.cn/2015-10-30_5632e1cd1f5fa.jpg) Copyright ©2013 David_Tang