小编Ave*_*ery的帖子

Postgres GROUP BY id 直接适用于表,但不适用于相同的视图

假设我有两张桌子。其中一个以一对多关系引用另一个:

CREATE TABLE t (
  id SERIAL PRIMARY KEY
, name text
);

INSERT INTO t (name) VALUES ('one'), ('two'), ('three');

CREATE TABLE y (
  id SERIAL PRIMARY KEY
, tid INTEGER REFERENCES t
, amount INTEGER
);

INSERT INTO y (tid, amount) VALUES
(1, 1),
(1, 2),
(1, 3),
(2, 1),
(3, 1);
Run Code Online (Sandbox Code Playgroud)

我可以从这些表中查询数据,以获得与 和t上设置的特定条件匹配的行:ty

SELECT row_to_json(t.*) as "t_nogroup" FROM t
JOIN y ON t.id = y.tid
WHERE t.id = 1
AND y.amount > 1; …
Run Code Online (Sandbox Code Playgroud)

postgresql view

4
推荐指数
1
解决办法
6114
查看次数

标签 统计

postgresql ×1

view ×1