这可能是一个非常基础的问题,但我在网上找不到任何答案。
如果我创建了一个样例表:
create table dummy ( id int not null, data json );
然后,如果我使用以下查询来查询表:
select * from dummy where data->'x' = 10;
现在由于表中还没有记录,并且任何记录中都没有名为“x”的属性,因此应该返回零结果。
但我收到了以下错误:
postgres=# select * from dummy where data->'x' = 10;
ERROR: operator does not exist: json = integer
LINE 1: select * from dummy where data->'x' = 10;
然而,以下查询有效:
select * from dummy where cast(data->>'x' as integer) = 10;
我是否漏掉了什么,或者强制类型转换是我从JSON字段获取整数值的唯一方法吗?如果是这种情况,当数据变得非常大时,它不会影响性能吗?
postgres=# select * from dummy where data->>'x' = '10';
id | data ----+----------- 1 | {"x": 10} (1 row)
postgres=# select * from dummy where data->>'x' = 10; ERROR: operator does not exist: text = integer LINE 1: select * from dummy where data->>'x' = 10; ^ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
- Mandeep Singh