小编Har*_*ish的帖子

PostgreSQL 在 JSON 数组中搜索值

我想在 PostgreSQL 中的 JSONB 中搜索元素,这里是我的 JSON

CREATE TABLE test
AS
  SELECT jsondata::jsonb
  FROM ( VALUES
    ( '{"key1": 1, "keyset": [10, 20, 30]}' ),
    ( '{"key1": 1, "keyset": [10, 20]}' ),
    ( '{"key1": 1, "keyset": [30]}' ),
    ( '{"key1": 1 }' ),
    ( '{"key1": 1, "key2": 1}' )
  ) AS t(jsondata);
Run Code Online (Sandbox Code Playgroud)

上表keyset中的所有行都不存在,我的查询是

SELECT * FROM test WHERE  jsondata->>'keyset' = 10;
Run Code Online (Sandbox Code Playgroud)

上面的查询给出了空结果,预期的输出是

jsondata
------------------------------------
{"key1": 1, "keyset": [10, 20, 30]}
{"key1": 1, "keyset": [10, 20]}
Run Code Online (Sandbox Code Playgroud)

postgresql json jsonb

3
推荐指数
1
解决办法
5663
查看次数

通过将 1 添加到现有键值来更新 PostgreSQL JSONB 键值

更新 JSON 数据时出现错误

CREATE TABLE testTable
AS
  SELECT $${
    "id": 1,
    "value": 100
  }$$::jsonb AS jsondata;
Run Code Online (Sandbox Code Playgroud)

我想value通过加 1更新到 101,在访问了许多网站后我发现了这个声明

UPDATE testTable
SET jsondata = JSONB_SET(jsondata, '{value}', (jsondata->>'value')::int + 1);
Run Code Online (Sandbox Code Playgroud)

但上面一个给出错误“无法将jsonb转换为int”

我的预期输出是

{
    "id": 1,
    "value": 101
}
Run Code Online (Sandbox Code Playgroud)

postgresql postgresql-9.5

1
推荐指数
1
解决办法
1242
查看次数

标签 统计

postgresql ×2

json ×1

jsonb ×1

postgresql-9.5 ×1