将JSONB数据迁移到列
我有一个带有以下模式的PostgreSQL数据库:
CREATE TABLE myrecords (data JSONB);
它具有一些看起来像这样的记录:
data
---------------------------------------------
{"field1": "enabled", "field2": "disabled"}
我想将数据从JSON BLOB迁移到SQL列;我像这样更改了表:
ALTER TABLE myrecords ADD COLUMN field1 BOOLEAN;
ALTER TABLE myrecords ADD COLUMN field2 BOOLEAN;
我设法使用jsonb_to_record
将JSON对象转换为行,但是我不知道如何(a)转换“启用”/“ disabled”字符串文字以布尔值为true/false值,(b)更新行以设置新的列值。是否可以同时更新一行?
I have a PostgreSQL database with the following schema:
CREATE TABLE myrecords (data JSONB);
It has some records that look like this:
data
---------------------------------------------
{"field1": "enabled", "field2": "disabled"}
I'd like to migrate the data from a JSON blob to SQL columns; I altered the table like so:
ALTER TABLE myrecords ADD COLUMN field1 BOOLEAN;
ALTER TABLE myrecords ADD COLUMN field2 BOOLEAN;
I've managed to convert the JSON object to a row using jsonb_to_record
, but I can't figure out how to (a) convert the "enabled"/"disabled" string literals to BOOLEAN TRUE/FALSE values, and (b) update the row to set the new column values. Is it possible to update a row while at the same time selecting data from it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
- >>
然后将结果比较以获取布尔值:You can use
->>
then compare the result to get a boolean value: