Sun*_*arg 2 sql postgresql json
我刚刚开始使用 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);
Run Code Online (Sandbox Code Playgroud)
但这正在显示
ERROR: invalid input syntax for integer: "{total_votes}"
LINE 1: SELECT (select poll_result::json#>'{total_votes}'::integer +...
Run Code Online (Sandbox Code Playgroud)
poll_result 的数据如下
{
"yes": 1,
"no": 0,
"total_votes": 1
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用打印total_votes时
RAISE NOTICE '%',poll_result::json#>'{total_votes};
Run Code Online (Sandbox Code Playgroud)
它打印 1。
即使我也尝试过
total_votes = (select (poll_result::json#>'{total_votes}')::integer + 1);
Run Code Online (Sandbox Code Playgroud)
但错误
ERROR: cannot cast type json to integer
LINE 1: ...ELECT (select (poll_result::json#>'{total_votes}')::integer ...
Run Code Online (Sandbox Code Playgroud)
该运算符#>给出一个 json,同时#>>给出一个文本,您需要第二个:
select (poll_result::json #>> '{total_votes}')::integer + 1
Run Code Online (Sandbox Code Playgroud)
或者
select (poll_result::json ->> 'total_votes')::integer + 1
Run Code Online (Sandbox Code Playgroud)
请参阅JSON 函数和运算符。
| 归档时间: |
|
| 查看次数: |
2508 次 |
| 最近记录: |