【发布时间】:2023-01-19 21:32:20
【问题描述】:
表一:
| id | status |
|---|---|
| 1 | 1 |
| 2 | 4 |
表 B:
| id | status | a_id |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 3 | 1 |
| 3 | 5 | 2 |
Table A (
id int,
status int);
Table B(
id int,
status int,
a_id int foreignt key reference A
);
当我在 (1,3) 中寻找状态时,如何进行返回此类输出的查询?
| id | status | arrayjson |
|---|---|---|
| 1 | 1 | [{id=1,status=1,a_id=1},{id=2,status=3,a_id=1}] |
如果我在 ( 3 ) 中寻找状态,它应该返回:
| id | status | arrayjson |
|---|---|---|
| 1 | 1 | [{id=2,status=3,a_id=1}] |
如果我在 ( 4 ) 中寻找状态,它应该返回:
| id | status | arrayjson |
|---|---|---|
| 2 | 4 | [] |
如果我在 ( 5 ) 中寻找状态,它应该返回:
| id | status | arrayjson |
|---|---|---|
| 2 | 4 | [{id=2,status=4,a_id=2}] |
【问题讨论】:
-
你的例子表明
status是从表b中选择的。status = 4的情况不一致,因为这里的状态似乎是从表a中获取的 - 我希望这里没有行或来自a的所有行都以空数组作为输出 - 请澄清。
标签: sql json postgresql aggregation