我有一些带有User
json列的details
条记录,如下所示:
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}])
如何查询id
为2
的所有记录?
答案 0 :(得分:0)
感谢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")