How can I do less than, greater than in JSON Postgres fields?

Deadly 提交于 2020-05-26 03:49:39

问题


If I have some json:

id = 1, json = {'key':95}
id = 2, json = {'key':90}
id = 3, json = {'key':50}

Is there a way I can use Postgres fields to query for key greater than >= 90?


回答1:


Use the operator ->> (Get JSON object field as text), e.g.

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)    



回答2:


If you use postgres version >= 9.3, then you can:

select * from t
where (json->>'key')::numeric >= 90


来源:https://stackoverflow.com/questions/44531689/how-can-i-do-less-than-greater-than-in-json-postgres-fields

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!