How do I write a GROUP BY or HAVING clause in Diesel?

1.8k views Asked by At

I'm trying to convert the following SQL query into corresponding Rust Diesel code:

SELECT COUNT(*)
FROM BookStore
WHERE BookName IN ('Lord of the Rings', 'Hobbit')
GROUP BY StoreId
HAVING COUNT(DISTINCT BookName) = 2

I was able to translate it thus far into:

let bookNames = vec!["Lord of the Rings", "Hobbit"];

let subquery = bookStores::table
.select(count_star())
.filter(bookName.eq_any(bookNames));

which I believe translates to:

SELECT COUNT(*)
FROM BookStore
WHERE BookName IN ('Lord of the Rings', 'Hobbit')

I'm having trouble finding any Diesel equivalent for the GROUP BY and the HAVING SQL clauses. Do these clauses even exist in Diesel?

0

There are 0 answers