【问题标题】:rails where not clause is not returning the records when the column value is null?当列值为空时,rails where not 子句不返回记录?
【发布时间】:2015-12-26 14:57:18
【问题描述】:

我有以下查询。

punch_recs = emp.punch_in_outs.where(date: Date.today-14 .. Date.today-1).where.not('status' => "Travel")

status 列值不等于nil 时,上述查询正常工作。但是当status 列值为 nil 时,我没有得到那些记录。我没有得到这个查询有什么问题。我的要求是我想为那些记录获取这些记录,date 列值应该在规定范围内,status 列值不应该是“旅行”。(即使没有 status 我的意思是如果status 也为空,我想获取这些记录。)但是这个查询没有给出那些status 为空或空的记录。

【问题讨论】:

  • sql 的幕后黑手是什么?
  • 2.2.2 :082 > punch_recs = emp.punch_in_outs.where(date: Date.today-14 .. Date.today-1).where.not('status' => "Travel" ) PunchInOut Load (0.2ms) SELECT punch_in_outs.* FROM punch_in_outs WHERE punch_in_outs.employee_id = 1 AND (punch_in_outs.date BETWEEN '2015-12-12' AND '2015-12- 25') AND (punch_in_outs.status != '旅行')
  • 您确定特定用户的表中的 nil 值吗?

标签: ruby-on-rails sql-server where


【解决方案1】:

NULL 在 SQL 中是未知的,它不等于任何东西。您需要单独处理此案:

punch_recs = emp.punch_in_outs.
  where(date: Date.today-14 .. Date.today-1).
  where("status != 'Travel' OR status IS NULL")

【讨论】:

  • 是的。 NULL 不等于,或者 not 等于任何东西(包括它自己)。它是未知的。
猜你喜欢
  • 2014-09-03
  • 2011-09-07
  • 2012-06-08
  • 1970-01-01
  • 1970-01-01
  • 2014-08-12
  • 2011-08-12
  • 1970-01-01
  • 2022-01-19
相关资源
最近更新 更多