是否有一种简洁的方法可以在1个查询中多次选择PostgreSQL序列的nextval?这将是唯一返回的值。
Is there a concise way to select the nextval for a PostgreSQL sequence multiple times in 1 query? This would be the only value being returned.
例如,我想做一些简短而可爱的事情,例如:
For example, I would like to do something really short and sweet like:
SELECT NEXTVAL('mytable_seq', 3) AS id;并获取:
id ----- 118 119 120 (3 rows)推荐答案
select nextval('mytable_seq') from generate_series(1,3);
generate_series是一个函数,该函数返回由其参数配置的带序号的许多行。
generate_series is a function which returns many rows with sequential numbers, configured by it's arguments.
在上面的示例中,我们不在乎每行中的值,我们只使用generate_series作为行生成器。对于每一行,我们可以调用nextval。在这种情况下,它返回3个数字(nextvals)。
In above example, we don't care about the value in each row, we just use generate_series as row generator. And for each row we can call nextval. In this case it returns 3 numbers (nextvals).
您可以将其包装到函数中,但是由于查询时间短,我不确定它是否真的有意义。
You can wrap this into function, but I'm not sure if it's really sensible given how short the query is.
更多推荐
从PostgreSQL序列中选择多个ID
发布评论