Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upHelp translating SQL query to diesel #550
Comments
This comment has been minimized.
|
Ok, I think I found a solution for the inner query, but I am not so sure how to approach the second step. Currenlty it fails with let max_ordering = max(elems::ordering).aliased("max_ordering");
let grouped = elems::table
.with(max_ordering)
.select((elems::type, max_ordering))
.filter(elems::type.eq_any(vec![ElemType::Two, ElemType::Eight]))
.groub_by(elems::type)
.aliased("grouped");
let results: Vec<Elements> = elems::table
.with(grouped)
.filter(grouped.eq(elems::table.select((elems::type, elems::ordering))))
.get_results(connection)?;
|
This comment has been minimized.
|
I do want to expand on the aliases thing though. We don't have any mechanism for arbitrary aliases in SQL, as it's not necessary. I am on my phone on roaming internet so apologies if the code is poorly formatted or has typos. For example, you could do:
Which will generate the SQL
You may say "but that's less efficient!". It's not, though. If you look at the query plan it's identical to the aliased form. Query planners are smart. |
mujx commentedDec 22, 2016
•
edited
I have some trouble translating the following SQL query into diesel (mostly with the sub query). Basically I have a table named
Elements(id, type, ordering)and I want to return the rows with the maximum ordering for some of the types (in the example the types 2 and 8).On a side note it would really helpful if there were more complex examples on the docs or some recipes for common queries.