2016-08-29 4 views
4

.Ecto의 두 열 합계로 쿼리를 정렬하는 방법은 무엇입니까?</p> <pre><code>(from f in Foo, where: f.bar >= ^bar, order_by: f.cost1 + f.cost2, limit: 1) |> Repo.one </code></pre> <p>그러나 그것은 잘못된 표현에 대해 불평하여 순서를 좋아하지 않는다 : 이런 일을하려고

(from f in Foo, 
select: %{id: id, total: fragment("cost1 + cost2 as total")}, 
order_by: f.total, 
limit: 1) }> Repo.one 

열 "f.total"라고하지 않습니다이 존재하지 않습니다

또한이 시도했다.

이 쿼리를 만드는 방법에 대한 아이디어가 있으십니까?

답변

5

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 
+0

감사합니다. – DCameronMauch