Proper way to do many-to-many? #4404
Answered
by
weiznich
duckfromdiscord
asked this question in
Q&A
-
I'm basically asking for something that would be in the relations guide, except getting book IDs for each book that was created by only a set of authors, given a list of author IDs. The books returned would only be books created by all of those authors at once, with no other authors not in that list. I understand it would probably involve a join, and I was able to get this to work in normal SQL. |
Beta Was this translation helpful? Give feedback.
Answered by
weiznich
Dec 23, 2024
Replies: 1 comment 8 replies
-
Basically my query looks like this: SELECT b.book_id FROM books b
JOIN book_authors ba ON b.book_id = ba.book_id
WHERE (some kind of search)
GROUP BY b.book_id
HAVING COUNT(DISTINCT ba.book_author) = (SELECT COUNT(DISTINCT book_author)
FROM book_authors
WHERE book_id = b.book_id
AND book_author IN ({each book author id})
AND COUNT (DISTINCT ba.book_author) = {author count}; and I do not understand from the docs/examples how to translate this to diesel. |
Beta Was this translation helpful? Give feedback.
8 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm currently on mobile, so I cannot easily test this on my own.
What I can say from looking at the query you wrote is:
diesel::alias!
for that.diesel::select
which constructs a select query without from clause, therefore the first error message. You want to use a normal query construct there as well..single_value()
on the subquery to turn into an expression that can be used as right hand side of.eq()