gpt4 book ai didi

ruby-on-rails - 如何在文本字段rails postgresql中查询json数据

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

我有一个数据库表列(collection),data type text

我在collection中插入了json数据

这是我的json数据

{"name":"test","age":"25","country":"xxx"}

但是现在我该如何查询这个 json 数据。有没有如下选项

User.where(collection: :name) # Here name denotes test

最佳答案

由于 JSON 是一个字符串,您只能使用 SQLite/MySQL(或任何其他数据库)进行字符串比较。此外,尽量将数据类型保持为二进制

您可以执行:LIKE 操作以在您存储的 JSON 字符串中出现子字符串。

您正在进行的上述比较将不起作用,因为它仅用于在集合列中搜索名称,而您已在该列中存储了整个 JSON 字符串。

关于ruby-on-rails - 如何在文本字段rails postgresql中查询json数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19720695/

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