Sql 更新数组json的数据

Sql 更新数组json的数据,sql,postgresql,jsonb,Sql,Postgresql,Jsonb,我的桌子 如何在PostgreSQL中更新json数组的值?您可以使用从0开始的json数组索引作为路径: update MyTable data=jsonb_set(data, '{Session1}', 'xxx',true) 可以使用从0开始的json数组索引作为路径: update MyTable data=jsonb_set(data, '{Session1}', 'xxx',true) 数据是一个json数组,因此Session1的路径需要是{0,Session1},对于Devi

我的桌子


如何在PostgreSQL中更新json数组的值?

您可以使用从0开始的json数组索引作为路径:

update MyTable data=jsonb_set(data, '{Session1}', 'xxx',true)

可以使用从0开始的json数组索引作为路径:

update MyTable data=jsonb_set(data, '{Session1}', 'xxx',true)
数据是一个json数组,因此Session1的路径需要是{0,Session1},对于DeviceId1,类似地{0,DeviceId1}

这将使更新声明:

update my_table
set data = jsonb_set(data, '{0}', '{"Session1": "xxx", "DeviceId1": "yyy"}')
where id = 1
returning *;

 id |                                     data                                     
----+------------------------------------------------------------------------------
  1 | [{"Session1": "xxx", "DeviceId1": "yyy"}, {"Session2": "", "DeviceId2": ""}]
(1 row) 
数据是一个json数组,因此Session1的路径需要是{0,Session1},对于DeviceId1,类似地{0,DeviceId1}

这将使更新声明:

update my_table
set data = jsonb_set(data, '{0}', '{"Session1": "xxx", "DeviceId1": "yyy"}')
where id = 1
returning *;

 id |                                     data                                     
----+------------------------------------------------------------------------------
  1 | [{"Session1": "xxx", "DeviceId1": "yyy"}, {"Session2": "", "DeviceId2": ""}]
(1 row) 

谢谢您的回答。如果我想从mytable中选择data->session1='xxx'和data->deviceid='yyy'应该如何编写?类似于这样的内容:选择*from t where data>'{0,session1}'='xxx'和data>'{0,DeviceD1}'='yyy'谢谢您的回答。如果我想从mytable中选择data->session1='xxx'和data->deviceid='yyy'应该如何编写?类似于这样的内容:选择*from t where data>>'{0,session1}'='xxx'和data>'{0,DeviceD1}'='yyy'