且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Postgres中的嵌套json中查询对象

更新时间:2023-01-16 20:58:37

您需要jsonb_each遍历attrs列中的所有条目.它将返回键/值对,其中键是uuid,条目是您要检查的实际JSON结构.您可以将其与EXISTS条件结合使用:

You need jsonb_each to iterate over all entries in the attrs colum. It will return key/value pairs where the key is the uuid and the entry is your actual JSON structure you want to inspect. You can use that in conjunction with an EXISTS condition:

select u.*
from users u
where exists (select *
              from jsonb_each(u.attrs) as t(uid,entry)
              where t.entry ->> 'label' = 'Email'
                and t.entry ->> 'value' = 'example@test.com.tw')

在线示例: https://rextester.com/SHN95362