【发布时间】:2015-12-03 23:42:41
【问题描述】:
假设我有两张桌子:
用户组合
+--------+----------------+
| 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 |
+-------------------+
我无法通过Colors.id 加入CombinationID。当我尝试 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 不存在。
有什么建议吗?
【问题讨论】:
-
你的 Postgres 版本?
-
使用 9.3.3。令人惊讶的是,unnest 不起作用。
-
您能解决问题中的一些令人困惑的错误吗?
json_array_elements(color)?Elements指的是什么?显示数据类型和约束的实际表定义会更有用。 -
可惜你没有Postgres 9.4,里面引入了
json_array_elements_text():dba.stackexchange.com/a/54289/3684这样会更方便。但是,当您坚持使用 Postgres 9.3 时,at least uprgrade to the latest point release。你的已经过时了。 -
感谢您的提示,欧文!会的。
标签: sql json postgresql set-returning-functions