Elixir 日期范围的EXTO查询
我必须根据模型中的时间戳查询日期范围。 到目前为止,我所尝试的是Elixir 日期范围的EXTO查询,elixir,phoenix-framework,ecto,Elixir,Phoenix Framework,Ecto,我必须根据模型中的时间戳查询日期范围。 到目前为止,我所尝试的是 str="2017-12-18" {:ok, ed}=Ecto.Date.cast(str) ed= ed |> Ecto.Date.from_erl |> NaiveDate.from_erl! Repo.all(from l in Log, where: l.inserted_at == ^ed) 然而,postgres中的日期以日期时间的形式出现,例如2017-12-18 19:58:01.414 但我只想查
str="2017-12-18"
{:ok, ed}=Ecto.Date.cast(str)
ed= ed |> Ecto.Date.from_erl |> NaiveDate.from_erl!
Repo.all(from l in Log, where: l.inserted_at == ^ed)
然而,postgres中的日期以日期时间的形式出现,例如2017-12-18 19:58:01.414
但我只想查询日期,而不是时间。但是,如果我使用exto.DateTime
和NaiveDateTime
并以字符串形式传递准确的日期时间,它就会工作。但我只想在日期上匹配。这怎么可能呢?
谢谢您可以使用
片段将字段强制转换为日期,然后比较:
Repo.all(from l in Log, where: fragment("?::date", l.inserted_at) == ^ed)
您可以使用片段将字段强制转换为日期,然后比较:
Repo.all(from l in Log, where: fragment("?::date", l.inserted_at) == ^ed)
ed |>exto.Date.from |>NaiveDate.from |erl代码>不能是真正的代码,请更正问题。ed |>exto.Date.from|erl |>NaiveDate.from|erl
不能是真正的代码,请更正问题。谢谢,我如何在无绑定操作中使用它?我不认为这可以通过无绑定查询实现,因为LHS是一个片段。谢谢,我如何在无绑定操作中使用它?我不认为这可以通过无绑定查询实现,因为LHS是一个片段。