Elixir,Ecto 比较 SQL 查询中的日期时间
Elixir, Ecto compare datetime in SQL query
我对 ecto 查询有疑问。我有这个功能:
def get_critials() do
critical_time = DateTime.to_naive(Timex.shift(Timex.now, seconds: -600))
query = "SELECT d.*"
<> " FROM sc_devices AS d"
<> " INNER JOIN log_device_commands AS ldc ON ldc.device_id = d.id"
<> " WHERE ldc.inserted_at < timestamp '#{critical_time}'"
{:ok, result} = Ecto.Adapters.SQL.query(Repo, query, [], [:rows])
result.rows
end
我想要的是从 table sc_devices
中获取所有记录,其中 log_device_commands
中的列 updated_at
早于 600 秒,但我得到了输出:
我收到了这个输出:
[
[1, "LAMP 1XX_1", "1.st Lamp on the corner", 1,
"6c7572e1-460f-43dd-b137-90c21d33525b", "XCA190SS2020DE", 3, 1, 1, 46.55472,
15.64667, true, nil, ~N[2020-11-12 20:32:22.000000],
~N[2020-11-12 20:32:22.000000], 2],
[1, "LAMP 1XX_1", "1.st Lamp on the corner", 1,
"6c7572e1-460f-43dd-b137-90c21d33525b", "XCA190SS2020DE", 3, 1, 1, 46.55472,
15.64667, true, nil, ~N[2020-11-12 20:32:22.000000],
~N[2020-11-12 20:32:22.000000], 2],
[1, "LAMP 1XX_1", "1.st Lamp on the corner", 1,
"6c7572e1-460f-43dd-b137-90c21d33525b", "XCA190SS2020DE", 3, 1, 1, 46.55472,
15.64667, true, nil, ~N[2020-11-12 20:32:22.000000],
~N[2020-11-12 20:32:22.000000], 2]
]
有什么解决办法吗?
您可以使用 postgres CURRENT_TIMESTAMP - INTERVAL '600 seconds'
而不是在查询中使用 elixir 变量。
此外,我看到您评论说您想按 updated_at
进行过滤,但您的查询实际上是按 inserted_at
.
进行过滤
我对 ecto 查询有疑问。我有这个功能:
def get_critials() do
critical_time = DateTime.to_naive(Timex.shift(Timex.now, seconds: -600))
query = "SELECT d.*"
<> " FROM sc_devices AS d"
<> " INNER JOIN log_device_commands AS ldc ON ldc.device_id = d.id"
<> " WHERE ldc.inserted_at < timestamp '#{critical_time}'"
{:ok, result} = Ecto.Adapters.SQL.query(Repo, query, [], [:rows])
result.rows
end
我想要的是从 table sc_devices
中获取所有记录,其中 log_device_commands
中的列 updated_at
早于 600 秒,但我得到了输出:
我收到了这个输出:
[
[1, "LAMP 1XX_1", "1.st Lamp on the corner", 1,
"6c7572e1-460f-43dd-b137-90c21d33525b", "XCA190SS2020DE", 3, 1, 1, 46.55472,
15.64667, true, nil, ~N[2020-11-12 20:32:22.000000],
~N[2020-11-12 20:32:22.000000], 2],
[1, "LAMP 1XX_1", "1.st Lamp on the corner", 1,
"6c7572e1-460f-43dd-b137-90c21d33525b", "XCA190SS2020DE", 3, 1, 1, 46.55472,
15.64667, true, nil, ~N[2020-11-12 20:32:22.000000],
~N[2020-11-12 20:32:22.000000], 2],
[1, "LAMP 1XX_1", "1.st Lamp on the corner", 1,
"6c7572e1-460f-43dd-b137-90c21d33525b", "XCA190SS2020DE", 3, 1, 1, 46.55472,
15.64667, true, nil, ~N[2020-11-12 20:32:22.000000],
~N[2020-11-12 20:32:22.000000], 2]
]
有什么解决办法吗?
您可以使用 postgres CURRENT_TIMESTAMP - INTERVAL '600 seconds'
而不是在查询中使用 elixir 变量。
此外,我看到您评论说您想按 updated_at
进行过滤,但您的查询实际上是按 inserted_at
.