插入值(generate_series) - 如何重用/循环数字,例如 1,2,3,1,2,3
我正在使用generate_series 在表中插入值。并且generate_series在其范围内插入指定的值。
例如: 对于以下查询,
SELECT i AS id, i AS age, i AS house_number
INTO egg
FROM generate_Series(1,6) AS i;
我们得到的结果是:
id age house_number
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
但我的问题是,我只想插入到“age”列中的数字 3,然后从 3 之后从 0 开始:
id age house_number
1 1 1
2 2 2
3 3 3
4 1 4
5 2 5
6 3 6
这可能吗? generate_series()
中是否有一些执行相同功能的随机函数?
I am using generate_series to insert values in a table. And generate_series insert values as specified in its range.
For example:
for the following query,
SELECT i AS id, i AS age, i AS house_number
INTO egg
FROM generate_Series(1,6) AS i;
the result we get is:
id age house_number
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
But my problem is, I want to insert only till number 3 in column "age" and then start from 0 after 3:
id age house_number
1 1 1
2 2 2
3 3 3
4 1 4
5 2 5
6 3 6
Is this possible? Are there some random functions in generate_series()
which performs the same function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用模运算从0循环到n - 1并加一:
You can use the modulo operation to cycle from 0 to n - 1 and add one:
您可以使用旨在实现此目的的序列:
You could use a sequence designed to do just that: