Issue
This is my table on PostgreSQL with name contacts:
- I want to edit mobile1 value with this sql query:
update contacts->info set mobile1 = JSON_SET(mobile1, "123456") where id=5
but that says :: ERROR: syntax error at or near "->"
- and when i want to delete or add a value with this sql query:
delete orders->info->mobile2 where id=5
syntax error at or near "orders"
- Or ADD
update orders->info set mobile3 = JSON_SET(mobile3, "123456") where id=5
syntax error at or near "->"
What's my syntax problem? and how can I do add, update and delete on my json datatype table on PostgreSQL
Solution
According to Postgres document for insert, update, or delete you should use JSONB
operation or function.
- Update scenario:
update contacts
set info = jsonb_set(info::jsonb, '{mobile,mobile1}', '"123456"')::json
where id = 5;
- Delete scenario:
update contacts
set info = (info::jsonb #- '{mobile,mobile2}')::json
where id = 5;
- Add scenario:
update contacts
set info = jsonb_set(info::jsonb, '{mobile,mobile3}', '"123456"')::json
where id = 5;
Answered By - Pooya Answer Checked By - Cary Denson (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.