sql - Postgres:如何在不知道键的情况下搜索 json 类型列中对象的 _all_ 字段上的值
问题描述
在 Postgres 数据库中,我有一个 jsonb 类型的列,其中包含一个我无法预测其键的通用对象:
{"some_key":"value", "another_unpredictable_key":"another value}
是否可以在不知道键的情况下在所有字段中搜索特定值?某事。喜欢
select * from ... where column_whatever->>'*' = '...'
解决方案
您需要将 json 值转换为多行(键/值对)并在结果中搜索:
select *
from some_table t
where exists (select *
from jsonb_each_text(t.jsonb_column) as x(ky,val)
where x.val = 'some value');
jsonb_each_text()
为每个顶级键/值对返回一行。这不处理嵌套键。
推荐阅读
- parameter-passing - 方法调用后传递结果值和变量值
- java - 在java中显示控制台输出到UI
- arrays - 基于两个数组字段在 VBA 中创建数组
- qt - 用于配置的 Qt 插件选项
- python - SpaCy在解析文本时没有分配正确的依赖标签
- javascript - 如何使用 .forEach 正确访问数组中的对象?
- openshift - 我无法再访问 OpenShift Online Starter 控制台
- exception - Cassandra3:线程异常[ReadRepairStage,操作超时-仅收到0个响应
- bash - 计算字符串中子字符串出现的次数
- alexa - Alexa Skill - 如何获取要求 Alexa 的完整陈述文本