Sql 获取按逗号/列表分隔的字符串形式的列名
我正在对我的一些表和所有字段进行一些插入,但有一些需要插入或更新。 有没有一种快速的方法可以将表中的列作为字符串返回,以便将它们复制到sql查询中? 例如,我有一个表:Sql 获取按逗号/列表分隔的字符串形式的列名,sql,postgresql,Sql,Postgresql,我正在对我的一些表和所有字段进行一些插入,但有一些需要插入或更新。 有没有一种快速的方法可以将表中的列作为字符串返回,以便将它们复制到sql查询中? 例如,我有一个表: create table myTable1 ( column1 integer not null, column2 integer not null , column3 integer not null, colum
create table myTable1
(
column1 integer not null,
column2 integer not null ,
column3 integer not null,
column4 integer not null,
column5 integer not null,
...a lot more fields...
)
稍后,我想从另一个具有相同字段的表myTable2
中插入一些内容(不要质疑我的方式以及为什么我有两个相同的表)
但是这些表有太多的字段,手工编写它们会很麻烦,如果有一个字符串,我可以删除我不需要的列名,这会更快。有没有一个很好的查询可以输出
“column1,column2,column3,column4,…”
,这样我就不必自己写了,可以把它复制到我的查询中?很快找到了答案
SELECT table_catalog, string_agg(column_name, ', ')
FROM information_schema.columns
WHERE table_schema = 'mySchema'
AND table_name = 'myTable' GROUP BY 1;
这个问题对我很有帮助
SELECT table_catalog, string_agg(column_name, ', ')
FROM information_schema.columns
WHERE table_schema = 'mySchema'
AND table_name = 'myTable' GROUP BY 1;