我怎样才能比JSON Postgres字段做得更少?

Kam*_*i81 6 postgresql jsonb

如果我有一些json:

id = 1, json = {'key':95}
id = 2, json = {'key':90}
id = 3, json = {'key':50}
Run Code Online (Sandbox Code Playgroud)

有没有办法我可以使用Postgres字段来查询大于> = 90的密钥?

kli*_*lin 8

使用运算符->> (将JSON对象字段作为文本),例如

with my_table(id, json) as (
values
(1, '{"key":95}'::json),
(2, '{"key":90}'),
(3, '{"key":50}')
)

select *
from my_table
where (json->>'key')::int >= 90;

 id |    json    
----+------------
  1 | {"key":95}
  2 | {"key":90}
(2 rows)    
Run Code Online (Sandbox Code Playgroud)


Oto*_*dze 5

如果您使用postgres版本> = 9.3,那么您可以:

select * from t
where (json->>'key')::numeric >= 90
Run Code Online (Sandbox Code Playgroud)