【问题标题】:Return records that don't have specific key - Rails4 HStore Postgresql query返回没有特定键的记录 - Rails4 HStore Postgresql 查询
【发布时间】:2013-07-15 00:56:59
【问题描述】:

我有一个模型任务

t.string   "name"
t.hstore   "actions"

例子:

#<Task id: 1, name: "first", actions: {"today"=>"9"}, 
#<Task id: 2, name: "second", actions: {"yesterday"=>"1"}, 
#<Task id: 3, name: "third", actions: nil,
#<Task id: 4, name: "four", actions: {"today"=>"11"},

我需要找到所有操作的记录:nil、:today

Task.where("actions -> 'today' < '10'") - it return only first task.
Task.where("actions -> 'today' < '10' OR actions IS NULL")  - it return 1 and 3 records.

我怎样才能找到所有没有关键的记录:today in action?

【问题讨论】:

  • 您的意思是您想要任务 2,3 而不是 1,2,3 任务作为回报吗?我看到今天出现在任务 1 中。
  • 我需要返回 1,2,3 个任务。
  • 你的行为中是否存在“前天”之类的东西?有哪些可能性,只是 todayyesterday 还是还有更多?
  • 更多。 Task.where("actions -> 'today'

标签: ruby-on-rails postgresql-9.1 hstore


【解决方案1】:

来自here,根据你的问题:

Task.where("actions IS NULL OR EXIST(actions, 'today') = FALSE")

根据你的 cmets:

Task.where("actions -> 'today' < '10' OR actions IS NULL OR EXIST(actions, 'today') = FALSE")

【讨论】:

  • 出色的答案正是所要求的。太棒了!
猜你喜欢
  • 2014-04-14
  • 1970-01-01
  • 1970-01-01
  • 2016-10-23
  • 2017-10-02
  • 2021-06-10
  • 1970-01-01
  • 2022-10-17
  • 1970-01-01
相关资源
最近更新 更多