(NULL) 中的 NULL 未正确匹配

发布于 2024-09-25 04:43:52 字数 1753 浏览 7 评论 0原文

使用 Rails 3 主动关系,我有一个范围:

  scope :duplicate_contact, lambda {|contact| where(
                  :person_id          => contact.person_id,
                  :salutation         => contact.salutation,
                  :first_name         => contact.first_name,
                  :last_name          => contact.last_name,
                  :suffix             => contact.suffix,
                  :birthday           => contact.birthday,
                  :address            => contact.address,
                  :city               => contact.city,
                  :state              => contact.state,
                  :zip                => contact.zip,
                  :phone_1            => [contact.phone_1,contact.phone_2,contact.phone_3],
                  :phone_1_type       => [contact.phone_1_type,contact.phone_2_type,contact.phone_3_type],
                  :phone_2            => [contact.phone_1,contact.phone_2,contact.phone_3],
                  :phone_2_type       => [contact.phone_1_type,contact.phone_2_type,contact.phone_3_type],
                  :phone_3            => [contact.phone_1,contact.phone_2,contact.phone_3],
                  :phone_3_type       => [contact.phone_1_type,contact.phone_2_type,contact.phone_3_type],
                  :email              => [contact.email,contact.alternate_email],
                  :alternate_email    => [contact.email,contact.alternate_email]
            )
          }

当 :email 为 NULL 时,这会出现问题。它返回零行,而实际上它应该返回至少 1 行,即 duplicate_contact(contact).size == 0 为 true,而它应该为 false。

我认为这与 mysql 文档中的这句话有关:“在 SQL 中,与任何其他值(甚至 NULL)相比,NULL 值永远不是真的。”

我怎样才能让它返回正确的结果?

Using Rails 3 active relation, I have a scope:

  scope :duplicate_contact, lambda {|contact| where(
                  :person_id          => contact.person_id,
                  :salutation         => contact.salutation,
                  :first_name         => contact.first_name,
                  :last_name          => contact.last_name,
                  :suffix             => contact.suffix,
                  :birthday           => contact.birthday,
                  :address            => contact.address,
                  :city               => contact.city,
                  :state              => contact.state,
                  :zip                => contact.zip,
                  :phone_1            => [contact.phone_1,contact.phone_2,contact.phone_3],
                  :phone_1_type       => [contact.phone_1_type,contact.phone_2_type,contact.phone_3_type],
                  :phone_2            => [contact.phone_1,contact.phone_2,contact.phone_3],
                  :phone_2_type       => [contact.phone_1_type,contact.phone_2_type,contact.phone_3_type],
                  :phone_3            => [contact.phone_1,contact.phone_2,contact.phone_3],
                  :phone_3_type       => [contact.phone_1_type,contact.phone_2_type,contact.phone_3_type],
                  :email              => [contact.email,contact.alternate_email],
                  :alternate_email    => [contact.email,contact.alternate_email]
            )
          }

This has a problem when :email is NULL. It returns back zero rows, when in fact it should return at least 1 row, ie duplicate_contact(contact).size == 0 is true when it should be false.

I think this has to do with this staement from the mysql docs: "In SQL, the NULL value is never true in comparison to any other value, even NULL."

How can I get this to return the correct result?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆梦 2024-10-02 04:43:52

我发现的一种可能的解决方案:

 scope :duplicate_contact, lambda {|contact| 

    q = where(
      :person_id          => contact.person_id,
      :salutation         => contact.salutation,
      :first_name         => contact.first_name,
      :last_name          => contact.last_name,
      :suffix             => contact.suffix,
      :birthday           => contact.birthday,
      :address            => contact.address,
      :city               => contact.city,
      :state              => contact.state,
      :zip                => contact.zip
    )
    [contact.phone_1,contact.phone_2,contact.phone_3].compact.each{|p| q=q.has_phone(p)}
    [contact.phone_1_type,contact.phone_2_type,contact.phone_3_type].compact.each{|p| q=q.has_phone_type(p)}
    [contact.email,contact.alternate_email].compact.each{|p| q=q.has_email(p)}
    q
  }
  scope :has_phone, lambda {|phone|
    where("'#{phone}' IN (phone_1,phone_2,phone_3)")
  }
  scope :has_phone_type, lambda {|phone|
    where("'#{phone}' IN (phone_1_type,phone_2_type,phone_3_type)")
  }
  scope :has_email, lambda {|email|
    where("'#{email}' IN (email,alternate_email)")
  }

One possible solution that I found:

 scope :duplicate_contact, lambda {|contact| 

    q = where(
      :person_id          => contact.person_id,
      :salutation         => contact.salutation,
      :first_name         => contact.first_name,
      :last_name          => contact.last_name,
      :suffix             => contact.suffix,
      :birthday           => contact.birthday,
      :address            => contact.address,
      :city               => contact.city,
      :state              => contact.state,
      :zip                => contact.zip
    )
    [contact.phone_1,contact.phone_2,contact.phone_3].compact.each{|p| q=q.has_phone(p)}
    [contact.phone_1_type,contact.phone_2_type,contact.phone_3_type].compact.each{|p| q=q.has_phone_type(p)}
    [contact.email,contact.alternate_email].compact.each{|p| q=q.has_email(p)}
    q
  }
  scope :has_phone, lambda {|phone|
    where("'#{phone}' IN (phone_1,phone_2,phone_3)")
  }
  scope :has_phone_type, lambda {|phone|
    where("'#{phone}' IN (phone_1_type,phone_2_type,phone_3_type)")
  }
  scope :has_email, lambda {|email|
    where("'#{email}' IN (email,alternate_email)")
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文