PostgreSQL-从jsonb字段复制值的格式错误
我有以下模式:PostgreSQL-从jsonb字段复制值的格式错误,sql,postgresql,Sql,Postgresql,我有以下模式: CREATE TABLE survey_results ( id integer NOT NULL, scores jsonb DEFAULT '{}'::jsonb ); INSERT INTO survey_results (id, scores) VALUES (1, '{"total": 10}'); INSERT INTO survey_results (id, scores) VALUES (2, '{"total": 10}');
CREATE TABLE survey_results (
id integer NOT NULL,
scores jsonb DEFAULT '{}'::jsonb
);
INSERT INTO survey_results (id, scores)
VALUES (1, '{"total": 10}');
INSERT INTO survey_results (id, scores)
VALUES (2, '{"total": 10}');
我想通过以下查询将total
key的值复制到risk score
key:
update survey_results set scores = jsonb_set(scores, '{risk-score}', to_jsonb(scores#>>'{total}'), true);
问题是复制的值是字符串而不是整数:
{"total": 10, "risk-score": "10"}
我怎样才能解决这个问题
只需将运算符更改为
#>
只需将运算符更改为
#>
使用
->
操作符:
update survey_results
set scores = jsonb_set(scores, '{risk-score}', scores->'total', true);
使用
->
操作符:
update survey_results
set scores = jsonb_set(scores, '{risk-score}', scores->'total', true);
您可以创建第二个JSON字典,并将其与
分数连接起来:
UPDATE survey_results SET scores = scores || FORMAT('{"risk-score":%s}', scores->>'total')::JSONB;
您可以创建第二个JSON字典,并将其与分数连接起来:
UPDATE survey_results SET scores = scores || FORMAT('{"risk-score":%s}', scores->>'total')::JSONB;
to_jsonb(scores#>'{total}')
毫无意义,因为参数已经是json了<代码>分数#>'{total}'
就足够了。这确实不是必需的。。这倒不是无稽之谈。to_jsonb(scores#>'{total}')
毫无意义,因为参数已经是json了<代码>分数#>'{total}'
就足够了。这确实不是必需的。。但这并不荒谬。