Skip to content
Advertisement

How to display values from different tables in certain time frame MYSQL

I have attached the tables that are included in this question for MYSql. My question states:

First and last names of the top 5 authors clients borrowed in 2017. My code so far:

SELECT BookID,BorrowDate COUNT(BookID) AS BookIDCount 
FROM Borrower    
WHERE BorrowDate = 2017
ORDER BY BookIDCount DESC
LIMIT 5

I think so far my code just displays the top 5 Author ID in 2017 but I can’t figure out how to display the names. I see the link between AuthorID and BookAuthor (maybe). Thank you so much for any help you may provide.

Here are the tables:

enter image description here

Advertisement

Answer

You can bring the client table with a join. I think that you want:

select c.clientFirstName, c.clientLastName, count(*) no_books
from borrower b
inner join client c on c.clientId = b.clientId
where b.borrowDate >= '2017-01-01' and b.borrowDate < '2018-01-01'
group by c.clientId, c.clientFirstName, c.clientLastName
order by count(*) desc
limit 5

This treats borrowDate as a column of type date (or the-like), because that what it seems to be. If it just a number that represent the year, then you can change back the where clause to your original condition.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement