从PostgreSQL序列中选择多个ID

编程入门 行业动态 更新时间:2024-10-23 10:33:23
本文介绍了从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

本文发布于:2023-10-14 21:56:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1492329.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   序列   PostgreSQL   ID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!