将集合返回函数 (json_array_elements) 的结果与 table 列联接
Join result of set-returning function (json_array_elements) with table column
假设我有两个表:
User_Combination
+--------+----------------+
| id | combination |
+--------+----------------+
| 6 | [1, 2] |
| 9 | [2, 3] |
+--------+----------------+
颜色
+--------+----------------+
| id | color |
+--------+----------------+
| 1 | Blue |
| 2 | Yellow |
| 3 | Green |
+--------+----------------+
我正在尝试将 json_array_elements(color)
的结果与 Elements 的 id
相结合。例如
的结果
select json_array_elements(color) as CombinationID
from User_Combination where id = 6;
是
+-------------------+
| CombinationID |
+-------------------+
| 1 |
| 2 |
+-------------------+
我无法加入 CombinationID
和 Colors.id
。当我尝试 SQL 命令时,例如:
select json_array_elements(article_data) AS articlesInOutfits (color) as CombinationID
from User_Combination uc JOIN Colors co ON co.id = articlesInOutfits;
或
select json_array_elements(article_data) AS articlesInOutfits (color) as CombinationID
from User_Combination uc JOIN Colors co ON co.id = uc.articlesInOutfits;
它说 articlesInOutfits
不存在。
有什么建议吗?
使用unnest()
得到解压组合:
select id, unnest(combination) cid
from user_combination;
id | cid
----+-----
6 | 1
6 | 2
9 | 2
9 | 3
(4 rows)
使用unpacked cids
加入colors
:
select u.id, color
from (
select id, unnest(combination) cid
from user_combination
) u
join colors c
on cid = c.id;
id | color
----+--------
6 | Blue
6 | Yellow
9 | Yellow
9 | Green
(4 rows)
使用聚合函数(例如 json_agg()
)为用户聚合合并的颜色:
select u.id, json_agg(color)
from (
select id, unnest(combination) cid
from user_combination
) u
join colors c
on cid = c.id
group by 1;
id | json_agg
----+---------------------
9 | ["Yellow", "Green"]
6 | ["Blue", "Yellow"]
(2 rows)
如果 combination
是类型 json
你应该在横向连接中使用 json_array_elements()
:
select u.id, json_agg(color)
from (
select id, cid
from user_combination,
lateral json_array_elements(combination) cid
) u
join colors c
on cid::text::int = c.id
group by 1;
假设我有两个表:
User_Combination
+--------+----------------+
| id | combination |
+--------+----------------+
| 6 | [1, 2] |
| 9 | [2, 3] |
+--------+----------------+
颜色
+--------+----------------+
| id | color |
+--------+----------------+
| 1 | Blue |
| 2 | Yellow |
| 3 | Green |
+--------+----------------+
我正在尝试将 json_array_elements(color)
的结果与 Elements 的 id
相结合。例如
select json_array_elements(color) as CombinationID
from User_Combination where id = 6;
是
+-------------------+
| CombinationID |
+-------------------+
| 1 |
| 2 |
+-------------------+
我无法加入 CombinationID
和 Colors.id
。当我尝试 SQL 命令时,例如:
select json_array_elements(article_data) AS articlesInOutfits (color) as CombinationID
from User_Combination uc JOIN Colors co ON co.id = articlesInOutfits;
或
select json_array_elements(article_data) AS articlesInOutfits (color) as CombinationID
from User_Combination uc JOIN Colors co ON co.id = uc.articlesInOutfits;
它说 articlesInOutfits
不存在。
有什么建议吗?
使用unnest()
得到解压组合:
select id, unnest(combination) cid
from user_combination;
id | cid
----+-----
6 | 1
6 | 2
9 | 2
9 | 3
(4 rows)
使用unpacked cids
加入colors
:
select u.id, color
from (
select id, unnest(combination) cid
from user_combination
) u
join colors c
on cid = c.id;
id | color
----+--------
6 | Blue
6 | Yellow
9 | Yellow
9 | Green
(4 rows)
使用聚合函数(例如 json_agg()
)为用户聚合合并的颜色:
select u.id, json_agg(color)
from (
select id, unnest(combination) cid
from user_combination
) u
join colors c
on cid = c.id
group by 1;
id | json_agg
----+---------------------
9 | ["Yellow", "Green"]
6 | ["Blue", "Yellow"]
(2 rows)
如果 combination
是类型 json
你应该在横向连接中使用 json_array_elements()
:
select u.id, json_agg(color)
from (
select id, cid
from user_combination,
lateral json_array_elements(combination) cid
) u
join colors c
on cid::text::int = c.id
group by 1;