Given these tables below
Authors Table
Books Table
| ID |
Title |
author_id |
| 1 |
Book1 |
1 |
| 2 |
Book2 |
1 |
| 3 |
Book3 |
2 |
Votes Table
| ID |
book_id |
Vote(like = 1, dislike = 0) |
| 1 |
1 |
1 |
| 2 |
1 |
1 |
| 3 |
2 |
0 |
query i composed
SELECT
name,
b.title,
MAX(upvote)
FROM
(SELECT
COUNT(IF(v.vote = 1, v.vote, NULL)) AS upvote,
b.id AS book_id,
b.author_id AS author_id
FROM
books b
LEFT JOIN votes v ON b.id = v.book_id
GROUP BY b.author_id, b.id
)AS X
LEFT JOIN books b ON x.book_id = b.id
INNER JOIN authors a ON x.author_id = a.id
GROUP BY a.id
Problem: Find the book with the most upvotes(like=1) per author.
im stuck on this problem for 2 hours or so now, i still couldnt get the title of the MAX(vote). Based on the table above, in the main SELECT clause, it must return Book1 but instead I get Book2.
Now how will I get the title of the MAX(vote)? I still couldn't figure out how to solve this one.