我在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) 在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) 我正在寻找以下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版本,我将非常感激.