我正在使用postgres Sql,我打算在表中插入一百万条记录。但是,对于其中一列,我想插入一个附加了数字并按顺序递增的值。例如:

在上面的示例中,pxinsname和key具有递增的值,并附加一个常量文本(S-,ABC )
我不确定如何才能递增这些列中的值。正在尝试使用以下查询-
insert into testable (
pxcommitdatetime,pxsavedatetime,pxcreatedatetime,pxcreateoperator,pxcreateopname,pxinsname,key,tripname, source, destination, passengername,passengerage
)
select
CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP,
test,
test,
// S- "Incremental value",
// ABC S "Incremental value",
md5(random()::text),
md5(random()::text),
md5(random()::text),
md5(random()::text),
left(md5(random()::text), 4)
from generate_series(1, 10) s(i)发布于 2021-05-04 13:14:21
为此,您可以使用序列:https://www.postgresql.org/docs/current/sql-createsequence.html
首先,您需要创建它:
CREATE SEQUENCE key_seq START 1;然后在查询中使用,如下所示:
insert into testable (
pxcommitdatetime,pxsavedatetime,pxcreatedatetime,pxcreateoperator,pxcreateopname,pxinsname,key,tripname, source, destination, passengername,passengerage
)
select
CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP,
test,
test,
// S- "Incremental value",
"ABC S" || nextval('key_seq')::text,
md5(random()::text),
md5(random()::text),
md5(random()::text),
md5(random()::text),
left(md5(random()::text), 4)
from generate_series(1, 10) s(i)https://stackoverflow.com/questions/67379105
复制相似问题