gpt4 book ai didi

ruby - 如何在 PostgreSQL 中使用 "LIKE"查询 jsonb 列类型?

转载 作者:数据小太阳 更新时间:2023-10-29 07:24:32 26 4
gpt4 key购买 nike

对于 PostgreSQL 数据库中的 hstore 列,我知道我可以像在 Ruby on Rails 中那样使用“LIKE”查询来搜索包含特定字符串的名称:

  Product.where("hstore_data -> 'author' LIKE '%billy%'")

我尝试了 jsonb 列类型,但出现了这个错误:

ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR: operator does not exist: jsonb ~~ unknown
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. : SELECT "products".* FROM "products" WHERE (jsonb_data -> 'author' LIKE '%billy%')

有没有办法为 jsonb 列类型正确使用“LIKE”?

最佳答案

你可以试试这个

希望你有

product.jsonb_data = {
author: "billynando"
}

然后

Product.where("jsonb_data ->> :key LIKE :value",
key: "author", value: "%billy%"
)

更多here

关于ruby - 如何在 PostgreSQL 中使用 "LIKE"查询 jsonb 列类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36540872/

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