相关疑难解决方法(0)

如何在json字段类型postgresql中查询空值?

我在postgresql中有一个json类型字段.但是,我不能选择特定字段为null的行:

码:

SELECT *
FROM   json_array_elements(
  '[{"name": "Toby", "occupation": "Software Engineer"},
    {"name": "Zaphod", "occupation": "Galactic President"} ,
{"name2": "Zaphod", "occupation2": null} ]'  ) AS elem
where elem#>'{occupation2}' is null
Run Code Online (Sandbox Code Playgroud)

这应该工作,但我收到此错误:

ERROR:  operator does not exist: json #> boolean
LINE 6: where elem#>'{occupation2}' is null
Run Code Online (Sandbox Code Playgroud)

sql postgresql json

40
推荐指数
2
解决办法
4万
查看次数

Ruby Rails,JSON - 如果密钥存在,则匹配结果

在postgres数据库中,设置是ruby on rails.调用该表并调用line_sourcesJSON列names.我想返回names列包含一个名为的键的所有行away_names.我正在尝试这个,但他们失败了:

LineSource.where("names -> 'away_names'")
Run Code Online (Sandbox Code Playgroud)

LineSource.where("names ->> 'away_names' = '%'")
Run Code Online (Sandbox Code Playgroud)

postgresql json ruby-on-rails

7
推荐指数
2
解决办法
3082
查看次数

如何使用JSON函数/运算符查询key是非null还是null?

我正在寻找以下sql代码的活动记录版本:

select *
from users
where (details->'email') is not null
Run Code Online (Sandbox Code Playgroud)

假设我的用户模型有一个叫做详细信息的json字段,有些记录会有一个电子邮件键值,有些则不会.我正在尝试查询具有电子邮件密钥值的用户记录.我尝试使用以下User.where("details - >'email'不为空"),但我的终端打印出来:

PG::UndefinedFunction: ERROR:  operator does not exist: json -> boolean
Run Code Online (Sandbox Code Playgroud)

我找到了这个SO帖子,试图做相反的查询,但想法是一样的.如果有人可以向我展示查询json密钥存在或不存在的Active Record版本,我将非常感激.

postgresql json rails-4-2-1

4
推荐指数
1
解决办法
910
查看次数

标签 统计

json ×3

postgresql ×3

rails-4-2-1 ×1

ruby-on-rails ×1

sql ×1