【问题标题】:invalid input syntax for integer pgsql整数 pgsql 的无效输入语法
【发布时间】: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


    【解决方案1】:

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

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

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

    JSON Functions and Operators.

    【讨论】:

      猜你喜欢
      • 2020-03-26
      • 1970-01-01
      • 2021-07-05
      • 1970-01-01
      • 2022-10-08
      • 1970-01-01
      • 2014-07-17
      • 2015-10-19
      • 2015-04-25
      相关资源
      最近更新 更多