从PostgreSQL序列中选择多个ID [英] Select multiple ids from a PostgreSQL sequence

查看:326
本文介绍了从PostgreSQL序列中选择多个ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简洁的方法可以在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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆