我正在尝试使用视图替换我们应用程序中的表。它在大多数情况下运行良好,但我无法克服这个错误:“列必须出现在 GROUP BY 子句中或用于聚合函数中”
重现步骤:
CREATE TABLE example_t (
did serial PRIMARY KEY,
a text,
b text
);
INSERT INTO example_t(a, b) VALUES ('a', 'b');
CREATE VIEW example_t_v AS
SELECT t.did as did, t.a as a, t.b as b
FROM example_t t;
Run Code Online (Sandbox Code Playgroud)
现在这个查询工作正常:
SELECT t.a, t.b FROM example_t t GROUP BY (t.did);
Run Code Online (Sandbox Code Playgroud)
但是来自视图的相同查询失败:
SELECT t.a, t.b FROM example_t_v t GROUP BY (t.did);
ERROR: column "t.a" must appear in the GROUP BY clause or be used in an aggregate function
LINE …
Run Code Online (Sandbox Code Playgroud)