【问题标题】:How to query using IN for matching emails?如何使用 IN 查询匹配的电子邮件?
【发布时间】:2012-09-27 23:06:41
【问题描述】:

给定一串电子邮件,例如:

emails = "atttt@bbb-inc.com, scott@sara.com, a@a.com, rasx@bo.com"

通过一个查询,我想返回所有匹配的用户记录,其中:

User.rb (id,email)

这是我的查询,但如果我将电子邮件设置为多个电子邮件,它不会返回结果:

User.where("email IN (?)", emails)
  User Load (1.0ms)  SELECT "users".* FROM "users" WHERE (users.deleted_at IS NULL) AND (email IN ('atttt@bbb-inc.com,, scott@sara.com, a@a.com, rasx@bo.com'))
 => [] 

有关如何更新此 User.where 查询以从提供的电子邮件中获取所有匹配用户的建议?谢谢。

【问题讨论】:

  • 添加 postgresql 作为标签,因为那是我使用 rails 的数据库。

标签: sql ruby-on-rails ruby-on-rails-3 postgresql


【解决方案1】:

首先,将emails变成一个数组:

emails = "atttt@bbb-inc.com, scott@sara.com, a@a.com, rasx@bo.com".split(', ')

然后你可以调用:

User.where(:email => emails)

【讨论】:

  • 谢谢,如果用户不在 emails 数组中,有什么办法可以扭转这种情况?谢谢
  • User.where("email NOT IN (?)", emails)
  • 应该可以的。您还记得先将电子邮件转换为数组吗?
猜你喜欢
  • 1970-01-01
  • 2011-04-28
  • 2016-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-20
  • 2015-09-16
  • 2017-06-15
相关资源
最近更新 更多