【问题标题】:Using JSON in postgres, how do I query a nested array? [duplicate]在 postgres 中使用 JSON,如何查询嵌套数组? [复制]
【发布时间】:2018-05-20 08:10:48
【问题描述】:

我有一些 User 记录,其中有一个 details json 列,如下所示:

User.create(details: {accounts: [{name: 'one', id: 1}, {name: 'two', id: 2}])
User.create(details: {accounts: [{name: 'three', id: 3}, {name: 'two', id: 2}])
User.create(details: {accounts: [{name: 'three', id: 3}, {name: 'four', id: 4}])

如何查询id2的所有记录?

【问题讨论】:

标签: ruby-on-rails json postgresql


【解决方案1】:

感谢Using jsonb (PostgreSQL), how do I retrieve items with a certain value that's saved as an array?让我走上了正轨。

要匹配的查询:

User.where("? in (SELECT json_array_elements(details->'accounts')->>'id')", "2")

【讨论】:

    猜你喜欢
    • 2017-10-19
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多