假设我有两张桌子。其中一个以一对多关系引用另一个:
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)