一尘不染

选择除某些PostgreSQL以外的所有列

sql

我必须比较表,但是有些列不需要比较,我只知道它们(不是我必须比较的列),所以我想从表中选择所有列,除了不需要的列比较。

我想到了类似的东西:

SELECT 'SELECT ' || array_to_string(ARRAY(SELECT 'o' || '.' || c.column_name
    FROM information_schema.columns As c
        WHERE table_name = 'office' 
        AND  c.column_name NOT IN('id', 'deleted')
), ',') || ' FROM officeAs o' As sqlstmt

但是输出SELECT * FROM office As o 不是select a,b,c from office没有id anddeleted列。

有谁知道这个查询有什么问题吗?


阅读 372

收藏
2021-05-16

共1个答案

一尘不染

这有效:

CREATE TABLE public.office(id INTEGER, a TEXT, b TEXT, deleted BOOLEAN, c TEXT)

然后:

SELECT 'SELECT ' || STRING_AGG('o.' || column_name, ', ') || ' FROM office AS o'
FROM information_schema.columns
WHERE table_name = 'office'
AND table_schema = 'public'
AND column_name NOT IN ('id', 'deleted')

结果:

SELECT o.a, o.b, o.c FROM office AS o

2021-05-16