【发布时间】:2020-11-27 22:40:51
【问题描述】:
查询:
SELECT string_agg(column_name::text, ',')
FROM information_schema.columns
WHERE table_name = 'my_table' and column_name !='id'
返回 my_table 中除 'id' 之外的列列表:
date_of_birth,first_name,last_name,e_mail
我想在定义列名时在 INSERT INTO 语句中使用这个列表。我应该怎么做,因为跟随不起作用。
INSERT INTO my_table2 (
SELECT string_agg(column_name::text, ',')
FROM information_schema.columns
WHERE table_name = 'my_table' and column_name !='id'
)
VALUES ('a', 'b', ...);
我在 my_table2 和 my_table 中更改了列数和顺序,所以我必须自动分配名称。
【问题讨论】:
-
my_table2 和 my_table 列数不匹配。
标签: postgresql insert assign