我想添加多个条目,其中1列保持不变,每次增加1列。要手动做到这一点,我必须进行
INSERT INTO table (column1, column2) VALUES ('1'::integer, '1'::integer)returning column1, column2;
INSERT INTO table (column1, column2) VALUES ('1'::integer, '2'::integer)returning column1, column2;
INSERT INTO table (column1, column2) VALUES ('1'::integer, '3'::integer)returning column1, column2;
etc
有没有一种方法可以在一个查询中查询数字1到34000?
使用generate_series()
:
INSERT INTO table (column1, column2)
SELECT 1, gs.n
FROM generate_series(1, 34000) gs(n);
注意:不需要将字符串转换为整数。一般来说,数字文字是可以的。在任何情况下,如果需要,Postgres都会将数字文本转换为正确的类型(比如int
、numeric
、float
或其他(。