List more borrowed books

Asked

Viewed 184 times

2

I would like to list the books in order of most borrowed, but there is no book that was borrowed so it shows none. I’d like you to show it in order but even if the book hasn’t been borrowed at all it has to be listed.

SELECT Livros.Nome, COUNT(Emprestimo.Livro_IDLivro) FROM Livros 
INNER JOIN Emprestimo ON Livros.IDLivro = Emprestimo.Livro_IDLivro 
ORDER BY COUNT(Emprestimo.Livro_IDLivro) ASC;
  • 1

    Jose, your problem is very simple. I suggest you read the post to understand the difference between the INNER and the LEFT: http://answall.com/questions/6441/qual%C3%A9-a-difference%C3%A7a-entre-Inner-Join-e-outer-Join

1 answer

3


To list all books you need to use LEFT JOIN instead of INNER JOIN, for the LEFT will bring you all the records from the table on the left (first table).

A to show what has more to what has less, you need to use the DESC in his ORDER BY

SELECT Livros.Nome, COUNT(Emprestimo.Livro_IDLivro) FROM Livros 
LEFT JOIN Emprestimo ON Livros.IDLivro = Emprestimo.Livro_IDLivro 
ORDER BY COUNT(Emprestimo.Livro_IDLivro) DESC;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.