gpt4 book ai didi

postgresql - Postgres : How to string pattern match query a json column?

转载 作者:行者123 更新时间:2023-12-05 01:43:19 24 4
gpt4 key购买 nike

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

select * from fooTable where myjson like "orld";

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

{ "foo": "hello", "bar": "world"}

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

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

最佳答案

使用 ->> operator以文本形式获取 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)

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

关于postgresql - Postgres : How to string pattern match query a json column?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49740223/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com