【问题标题】:Is it possible to find records in a table by value in object (without providing a key)?是否可以通过对象中的值(不提供键)在表中查找记录?
【发布时间】:2020-08-18 03:31:50
【问题描述】:

我有一张桌子profile,看起来像:

id  | name (jsonb)                       | lots of different fields ...
1   | {en: "Some name", fr: "Un nom"}    | ...
2   | {ru: "Какое то название"}          | ...

是否可以在表中找到值为Some name 的所有记录?

我使用 Postgresql 12

【问题讨论】:

  • 我不明白你的问题。也许使用一些示例数据和预期结果会更容易。
  • @LaurenzAlbe 我简化了我的问题。

标签: json postgresql indexing


【解决方案1】:

您需要取消嵌套 json 对象:

select p.*
from profile p
where exists (select *
              from jsonb_each(p.name) as j(k,v)
              where j.v = 'Some name');

在 Postgres 12 中,这也可以使用 JSON path 表达式来编写:

select p.*
from profile p
where name @? '$.keyvalue() ? (@.value == "Some name")'  

【讨论】:

    猜你喜欢
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 2017-05-10
    • 2016-11-28
    • 2016-04-07
    相关资源
    最近更新 更多