PostgreSQL jsonb value in WHERE BETWEEN clause

痞子三分冷 提交于 2019-12-02 07:27:19

The #> returns a JSONB document which you cannot cast to an int. You need the #>> operator which returns a scalar value that can be casted to an integer:

select *
from a_table
where (json_col #>> '{abc,def,ghk}')::int between 0 and 1000

All JSON operators are documented in the manual: http://www.postgresql.org/docs/current/static/functions-json.html

Using BETWEEN '0' AND '10000' is not a good idea because this does a string comparison not a numeric comparison. The value '2' does not lie between '0' and '10000'. That's why you need to cast the returned value to a number to get a correct comparison.

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