【问题标题】:Elixir / SQL - Converting a list to lower case before performing a query using itElixir / SQL - 在使用它执行查询之前将列表转换为小写
【发布时间】:2022-01-03 17:08:32
【问题描述】:

我正在尝试在我的数据库中搜索所有具有包含在给定电子邮件列表中的电子邮件的用户,与大小写无关。我的代码是:


  def get_user_ids_from_emails(emails) do
    emails = Enum.map(emails, fn email -> String.downcase(email) end)

    from(
      u in User,
      where:
        fragment(
          "LOWER(?) IN ?",
          u.email,
          ^emails
        ),
      select: u.id
    )
  end

当我尝试执行此操作时,我收到了

** (Postgrex.Error) ERROR 42601 (syntax_error) syntax error at or near "$1"
     
         query: SELECT u0."id" FROM "user" AS u0 WHERE (LOWER(u0."email") IN $1)

这是正确的方法吗?如果是这样,我该如何格式化列表以使其被 SQL 接受?

【问题讨论】:

  • 您的查询中没有括号吗? "LOWER(?) IN (?)"
  • 收到同样的错误:(

标签: sql elixir ecto


【解决方案1】:

来自Elixir Forum的回答,需要从片段中排除列表:

def get_user_ids_from_emails(emails) do
  emails = Enum.map(emails, fn email -> String.downcase(email) end)

  from(
    u in User,
    where:
      fragment("LOWER(?)", u.email) in ^emails,
    select: u.id
  )
end

【讨论】:

    猜你喜欢
    • 2010-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    相关资源
    最近更新 更多