PostgreSQL jsonb 计数其中键具有特定值的位置

问题描述 投票:0回答:1

如何过滤 jsonb 列对象中键数具有特定值的行?我有一个 jsonb 列,其中包含类似的内容

{
  "fri": "from_home",
  "mon": "from_home",
  "sat": "dont_work",
  "sun": "dont_work",
  "thu": "commute",
  "tue": "from_home",
  "wed": "commute"
}

我想要做的是获取此列将 3 天设置为“from_home”的所有行。我一直在研究 JSON 的各种 pg 函数,但我只是不知道什么是最合适的。

postgresql jsonb
1个回答
0
投票

您可以使用 jsonb_each_text 函数(https://www.postgresqltutorial.com/postgresql-json-functions/postgresql-jsonb_each_text/

select count(*), id from 
(select id, key, value from table_source, jsonb_each_text(col_json) sub1
where value='from_home' group by id having count(*)>3;
© www.soinside.com 2019 - 2024. All rights reserved.