【发布时间】:2014-03-07 15:56:35
【问题描述】:
目前第一次在 postgres 9.3 中使用 JSON 字段,我在查询数组时遇到了一些困难。
JSON 数组数据类型的字段称为“accounts”,一些示例数据如下
[{name: "foo", account_id: "123"}, {name: "bar", account_id: "321"}]
例如,我希望能够找到拥有 account_id 123 的公司的 ID。我目前遇到问题的查询如下:
select id from companies where json_array_elements(accounts)->>'account_id' = '123'
这会导致错误:
WHERE 的参数不能返回一个集合
【问题讨论】:
-
我猜你的意图更像是“如果 any of 元素有一个
account_id或123然后返回相应的公司 ID i>" ... ?.
标签: sql json postgresql psql