Postgresql-如何更改JSONB数据类型?
我有一个表,其中包含jsonb列“details”,数据类型错误,如下所示:Postgresql-如何更改JSONB数据类型?,postgresql,jsonb,Postgresql,Jsonb,我有一个表,其中包含jsonb列“details”,数据类型错误,如下所示: select id,details from products; id | details ---+----------------------------------------- 1 | {"price": "310", "supplier": "VendorA"} 2 | {"price": "250", "supplier": "VendorB"}
select id,details from products;
id | details
---+-----------------------------------------
1 | {"price": "310", "supplier": "VendorA"}
2 | {"price": "250", "supplier": "VendorB"}
在这里,我想将“price”的数据类型更改为整数,它当前存储为字符串。预期结果如下:
id | details
---+-----------------------------------------
1 | {"price": 310, "supplier": "VendorA"}
2 | {"price": 250, "supplier": "VendorB"}
如果您能指导我如何实现它,我将不胜感激。您可以使用函数
to_jsonb()
将值转换为json数字:
update语句可能如下所示:
update products
set details = jsonb_set(details, '{price}', to_jsonb((details->>'price')::int))
update products
set details = jsonb_set(details, '{price}', to_jsonb((details->>'price')::int))