【发布时间】:2018-04-21 22:42:23
【问题描述】:
我刚刚开始使用 postgresql。我在表中有一个 json 对象。 json 对象中有一个数值,我想在其中添加一个数字并将其分配给其他整数。我就是这样的
declare
total_votes integer;
....
select result into poll_result from polls where id = 1;
total_votes = (select poll_result::json#>'{total_votes}'::integer + 1);
但这是显示
ERROR: invalid input syntax for integer: "{total_votes}"
LINE 1: SELECT (select poll_result::json#>'{total_votes}'::integer +...
poll_result 有类似的数据
{
"yes": 1,
"no": 0,
"total_votes": 1
}
当我尝试使用打印 total_votes 时
RAISE NOTICE '%',poll_result::json#>'{total_votes};
它打印 1。
我也试过了
total_votes = (select (poll_result::json#>'{total_votes}')::integer + 1);
但是错误
ERROR: cannot cast type json to integer
LINE 1: ...ELECT (select (poll_result::json#>'{total_votes}')::integer ...
【问题讨论】:
标签: sql json postgresql