這是我在 PostgreSQL 上的帶有姓名聯系人的表:

- 我想用這個 sql 查詢編輯 mobile1 值:
update contacts->info set mobile1 = JSON_SET(mobile1, "123456") where id=5
但這說 :: 錯誤:“->”處或附近的語法錯誤
- 當我想用這個 sql 查詢洗掉或添加一個值時:
delete orders->info->mobile2 where id=5
“訂單”處或附近的語法錯誤
- 或添加
update orders->info set mobile3 = JSON_SET(mobile3, "123456") where id=5
“->”處或附近的語法錯誤
我的語法問題是什么?以及如何在 PostgreSQL 上的 json 資料型別表上添加、更新和洗掉
uj5u.com熱心網友回復:
根據用于插入、更新或洗掉的Postgres 檔案,您應該使用JSONB操作或函式。
演示
- 更新場景:
update contacts
set info = jsonb_set(info::jsonb, '{mobile,mobile1}', '"123456"')::json
where id = 5;
- 洗掉場景:
update contacts
set info = (info::jsonb #- '{mobile,mobile2}')::json
where id = 5;
- 添加場景:
update contacts
set info = jsonb_set(info::jsonb, '{mobile,mobile3}', '"123456"')::json
where id = 5;
uj5u.com熱心網友回復:
解決你的問題1。
更新json 欄位中的mobile1json 值info:
update contacts
set info = jsonb_set(info :: jsonb, '{mobile,mobile1}', '123456', true) :: json
where id=5
解決你的問題2。
mobile2從infojson 欄位中洗掉鍵/值對:
update contacts
set info = (info :: jsonb #- '{mobile,mobile2}') :: json
where id=5
從聯系人表中洗掉整行:
delete from contacts where id=5
您應該仔細閱讀手冊第 6 章資料操作和9.16。JSON 函式和運算子
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/380410.html
標籤:sql json PostgreSQL的 sql更新 sql数据类型
上一篇:從prometheus_exporter連接到資料庫
下一篇:在生成的列上處理pg_error
