如何按 Ecto 中两列的总和对查询进行排序?
How to order a query by sum of two columns in Ecto?
尝试做这样的事情:
(from f in Foo,
where: f.bar >= ^bar,
order_by: f.cost1 + f.cost2,
limit: 1) |> Repo.one
但是它不喜欢订单,抱怨无效的表达式。
也试过这个:
(from f in Foo,
select: %{id: id, total: fragment("cost1 + cost2 as total")},
order_by: f.total,
limit: 1) }> Repo.one
这不是说第 "f.total" 列不存在。
关于如何使这个查询起作用的任何想法?
我不确定为什么 Ecto 不支持 order_by: f.cost1 + f.cost2
,但是您的 fragment
语法不正确。这是正确的语法:
(from f in Foo,
where: f.bar >= ^bar,
order_by: fragment("? + ?", f.cost1, f.cost2),
limit: 1) |> Repo.one
尝试做这样的事情:
(from f in Foo,
where: f.bar >= ^bar,
order_by: f.cost1 + f.cost2,
limit: 1) |> Repo.one
但是它不喜欢订单,抱怨无效的表达式。
也试过这个:
(from f in Foo,
select: %{id: id, total: fragment("cost1 + cost2 as total")},
order_by: f.total,
limit: 1) }> Repo.one
这不是说第 "f.total" 列不存在。
关于如何使这个查询起作用的任何想法?
我不确定为什么 Ecto 不支持 order_by: f.cost1 + f.cost2
,但是您的 fragment
语法不正确。这是正确的语法:
(from f in Foo,
where: f.bar >= ^bar,
order_by: fragment("? + ?", f.cost1, f.cost2),
limit: 1) |> Repo.one