I have a problem to express the next statement on mySql to return true results:
select max(alltaken)mn, b.title from
(select count(bc.taken) as alltaken, b.title
from books_clients bc
join books b on b.book_id = bc.book_id
group by b.title) as mn
join books b on b.title = mn.title
I need to return from the first select the count of the taken books grouped by title and in the external select to select the max number but also to display the corresponding title of the book. Written like this the statement returns the max number and the title grouped by b.title, values which are not related…
The following part of the statement returns only the max number, but I cannot transform it to return the title too 🙁 :
select max(alltaken)mn
(select count(bc.taken) as alltaken, b.title
from books_clients bc
join books b on b.book_id = bc.book_id
group by b.title) as mn
Could you please help me to create the true query for my purpose.
You don’t even need a subquery:
If there are more than one results with same Max count, then you need a subquery: