[英]PostgreSQL: Remove value from JSONB column
我需要從jsonb類型列中刪除一些值。
jsonb值
{"services": [59, 61], "locations": ["34"]}
該查詢不起作用。
select jsonb_set(data, '{services}', (data->'services') - 59) from employees
WHERE data @> '{"services":[59]}';
但是,這可行:
select jsonb_set(data, '{locations}', (data->'locations') - '34') from
employees WHERE data @> '{"services":[59]}';
如何從沒有鍵索引和單引號的services
刪除59
?
(數據->“服務”)-59
按索引刪除項目
例
/* - 3 is the index to be deleted in a NUMERIC array 0 14 1 15 2 16 3 17 * to be deleted 4 18 */ select '[14, 15, 16, 17, 18]'::jsonb - 3
\n | ?柱? |\n | :--------------- |\n | [14、15、16、18] |\n
文本元素數組
CREATE TABLE employees ( id serial primary key, data jsonb );
\n ✓\n
insert into employees(data) values ('{"services": [50, 57, 59, 61], "locations": ["34"]}'::jsonb), ('{"services": [19, 21], "locations": ["34"]}'::jsonb) ;
\n 2行受影響\n
-- The link was used (https://dba.stackexchange.com/questions/54283/how-to-turn-json-array-into-postgres-array) select employees.id, jsonb_set(data, '{services}', ('[' || string_agg('"' || elem::text || '"' ,' ,') || ']')::jsonb - '59') from employees, json_array_elements((data->'services')::json) elem WHERE data @> '{"services":[59]}' group by employees.id ;
\n id | jsonb_set \n -:| :------------------------------------------------- ---\n 1 | {“服務”:[“ 50”,“ 57”,“ 61”],“位置”:[“ 34”]}\n
db <> 在這里撥弄
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.