generate_series()

生成一系列值的函数

generate_series() 是一个用于生成一系列值(数值型或时间型)的系统函数。

generate_series()PostgreSQL 8.0 中添加。

用法

generate_series ( start integer, stop integer [, step integer ] ) → setof integer 
generate_series ( start bigint, stop bigint [, step bigint ] ) → setof bigint
generate_series ( start numeric, stop numeric [, step numeric ] ) → setof numeric
generate_series ( start timestamp, stop timestamp, step interval ) → setof timestamp

PostgreSQL 16 及更高版本

generate_series ( start timestamp with time zone, stop timestamp with time zone, step interval [, timezone text ] ) 
        → setof timestamp with time zone

PostgreSQL 15 及更早版本

generate_series ( start timestamp with time zone, stop timestamp with time zone, step interval )
         → setof timestamp with time zone

变更历史

示例

generate_series() 的基本执行

postgres=# SELECT generate_series(1, 5);
 generate_series 
-----------------
               1
               2
               3
               4
               5
(5 rows)

生成降序序列

postgres=# SELECT * FROM generate_series(5, 1, -1);
 generate_series 
-----------------
               5
               4
               3
               2
               1
(5 rows)

生成一系列时间戳

postgres=# SELECT *
             FROM generate_series(current_timestamp,
                                  current_timestamp + '5 minutes'::interval,
                                 '1 minute'::interval);
        generate_series        
-------------------------------
 2021-02-08 09:05:21.455263+01
 2021-02-08 09:06:21.455263+01
 2021-02-08 09:07:21.455263+01
 2021-02-08 09:08:21.455263+01
 2021-02-08 09:09:21.455263+01
 2021-02-08 09:10:21.455263+01
(6 rows)

分类

系统函数

另请参阅

generate_subscripts()

反馈

提交任何关于 "generate_series()" 的评论、建议或更正 此处