【发布时间】:2014-07-25 11:20:17
【问题描述】:
这可能是一个非常基本的问题,但我无法在网上找到任何内容。
如果我创建一个示例表:
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. -
要详细说明我想说的话,如果我插入另一条 id = 10 的记录并在表
select * from dummy a, dummy b where a.id = b.data->>'x';上执行自联接,我仍然会遇到同样的错误。本质上,必须进行类型转换才能比较整数值
标签: sql json database postgresql postgresql-9.3