如何在JSON Postgres字段中做小于,大于做的事情? [英] How can I do less than, greater than in JSON Postgres fields?

查看:447
本文介绍了如何在JSON Postgres字段中做小于,大于做的事情?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一些json:

If I have some json:

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

有没有一种方法可以使用Postgres字段来查询大于等于90的键?

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

推荐答案

使用运算符->> (以文本形式获取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)    

这篇关于如何在JSON Postgres字段中做小于,大于做的事情?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆