简体   繁体   English

如何在字符串中间使用 SQL 中的 generate_series()?

[英]How do I use generate_series() in SQL in the middle of a string?

How do I use generate_series() in SQL in the middle of a string?如何在字符串中间使用 SQL 中的 generate_series()?

Ex: 'This is the 1 string.'例如:“这是第一个字符串。” 'This is the 2 string.' '这是 2 字符串。 'This is the 3 string.' '这是第 3 串。

I'm looking to generate numbers from 1 to 10000, so I know I would use generate_series(1, 10000), but how can I use this in the middle of a string?我正在寻找从 1 到 10000 的数字,所以我知道我会使用 generate_series(1, 10000),但是如何在字符串中间使用它呢?

I'm trying to insert this into table 'xyz' column 'strings'.我正在尝试将其插入到表“xyz”列“字符串”中。

I have tried this code but it obviously gave me an error: INSERT INTO xyz (strings) 'This is the ' ||我试过这段代码,但它显然给了我一个错误: INSERT INTO xyz (strings) 'This is the ' || generate_series(1, 10000) ||生成系列(1, 10000) || ' string.'; ' 细绳。';

Here is a way to do generate numbers(20) and insert each one in a separate row.这是一种生成数字(20)并将每个数字插入单独行的方法。

create table t(x varchar(100))

insert into t 
select 'This is ' || cast(x.* as varchar(20)) || ' string'
  from generate_series(1,20) x

https://dbfiddle.uk/?rdbms=postgres_14&fiddle=b646bd97e9aa3a8bb131dbb0335953ca https://dbfiddle.uk/?rdbms=postgres_14&fiddle=b646bd97e9aa3a8bb131dbb0335953ca

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM