gpt4 book ai didi

sql - 整数 pgsql 的输入语法无效

转载 作者:行者123 更新时间:2023-11-29 12:52:54 25 4
gpt4 key购买 nike

我刚刚开始使用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 ...

最佳答案

运算符#>给出一个json,而#>>给出一个文本,你需要第二个:

select (poll_result::json #>> '{total_votes}')::integer + 1

select (poll_result::json ->> 'total_votes')::integer + 1

参见JSON Functions and Operators.

关于sql - 整数 pgsql 的输入语法无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49956762/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com