Answer the question
In order to leave comments, you need to log in
Explain how multiple left joins work?
There are several tables.
Books with id and name fields.
Genres with id and name fields.
Ratings with userid rating and bookid fields Book
genres with bookid and genreid fields.
The resulting query should give such a table.
name | rating | urating | genre name |
some | 8 | 5 | g1, g2, gn|
But the request:
SELECT
b.name,
AVG(br.rating) as rating,
MAX(ubr.rating) as urating,
GROUP_CONCAT(g.name) as genrename
FROM books b
LEFT JOIN book_rating br ON br.book_id = b.id
LEFT JOIN book_rating ubr ON ubr.book_id = b.id AND ubr.user_id = 1
LEFT JOIN book_genre bg ON bg.book_id = b.id
LEFT JOIN genre g ON g.id = bg.genre_id
WHERE b.id = 1
GROUP BY b.id
Answer the question
In order to leave comments, you need to log in
Good!
There can be several ratings for one book, genres, as I understand it, too, here the lines are doubled, to see this, just remove the grouping and run the request. This will help you understand what's going on. Better yet, remove the grouping and join the tables one by one, analyzing the result, everything will become clear.
If you explain on your fingers, then all the ratings of this book will be attached to each book id
book | rating
---------------
1 | 4
1 | 5
book | rating | gename
----------------------
1 | 4 | g1
1 | 4 | g2
1 | 5 | g1
1 | 5 | g2
book | rating | gename
----------------------
1 | 4.5 | g1, g2, g1, g2
book | rating | gename
----------------------
1 | 4.5 | g1, g2
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question