我必须比较表,但是有些列不需要比较,我只知道它们(不是我必须比较的列),所以我想从表中选择所有列,除了不需要的列比较。
我想到了类似的东西:
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列。
SELECT * FROM office As o
select a,b,c from office
id anddeleted
有谁知道这个查询有什么问题吗?
这有效:
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