【问题标题】:How to create a SQL using 'between' in Elixir Ecto如何在 Elixir Ecto 中使用“between”创建 SQL
【发布时间】:2015-05-19 07:24:50
【问题描述】:

我想使用 Elixir Ecto 中的“between”键创建一个 SQL。

我知道如何使用like创建一个sql

where: like(t.descript, ^some_description)

但是当我尝试以与like相同的方式进行操作时

where: between(t.start_date, ^start_date, ^end_date),

我收到“无效”错误消息

** (Ecto.Query.CompileError) `between(t.start_date(), ^start_date, ^end_date)` is not a valid query expression.**

我怎样才能以正确的方式做到这一点?

提前致谢!!

【问题讨论】:

    标签: between elixir ecto


    【解决方案1】:

    我不认为 Ecto 提供了between 子句。你可以通过使用来完成你的任务

    where: t.start_date >= ^start_date,
    where: t.start_date <= ^end_date
    

    【讨论】:

    • 你的意思是start_date &lt;= ^end_date?
    • 对于其他人,您可以将它们与and 组合成一行。 where: t.start_date &gt;= ^start_date and t.start_date &lt;= ^end_date.
    【解决方案2】:

    您可以使用fragment 来执行此操作。

    where: fragment("? BETWEEN ? AND ?", t.date, ^start_date, ^end_date)
    

    https://hexdocs.pm/ecto/3.1.4/Ecto.Query.API.html#fragment/1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多