Postgres:如何字符串模式匹配查询 json 列?

irr*_*lar 3 postgresql

我有一个 json 类型的列,但我想知道如何选择过滤它,即

select * from fooTable where myjson like "orld";
Run Code Online (Sandbox Code Playgroud)

我将如何查询像上面这样的子字符串匹配。比如说在“bar”键下搜索“orld”?

{ "foo": "hello", "bar": "world"}
Run Code Online (Sandbox Code Playgroud)

我看了一下这个文档,但它很令人困惑。

https://www.postgresql.org/docs/current/static/datatype-json.html

kli*_*lin 7

使用->>运算符获取 json 属性作为文本,示例

with my_table(id, my_json) as (
values 
    (1, '{ "foo": "hello", "bar": "world"}'::json),
    (2, '{ "foo": "hello", "bar": "moon"}'::json)
)

select t.*
from my_table t
where my_json->>'bar' like '%orld'

 id |              my_json              
----+-----------------------------------
  1 | { "foo": "hello", "bar": "world"}
(1 row)
Run Code Online (Sandbox Code Playgroud)

%请注意,您需要在模式中使用占位符。