簡體   English   中英

PostgreSQL-將數值從jsonb字段復制到整數列

[英]PostgreSQL - copying numeric value from jsonb field to integer column

我有以下架構:

CREATE TABLE survey_results (
    id integer NOT NULL,
    publish_id integer,  
    raw jsonb DEFAULT '{}'::jsonb
);

INSERT INTO survey_results (id, raw)
    VALUES (1, '{"survey": {"publish_id": 5}}');

我想使用以下更新將值從jsonb字段復制到整數列:

UPDATE survey_results SET publish_id = raw#>>'{survey, publish_id}'; 

但這返回了我:

ERROR: column "publish_id" is of type integer but expression is of type text Hint: You will need to rewrite or cast the expression. Position: 40

我該如何解決?

http://sqlfiddle.com/#!17/c50f2/4

剛投

UPDATE survey_results SET publish_id = (raw#>>'{survey, publish_id}')::int;         

https://www.postgresql.org/docs/current/static/functions-json.html

以指定的路徑獲取JSON對象作為文本

因此您需要將文本轉換為整數以匹配列數據類型

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM