1
Hello! I was working on a database project using the SQL language in Mysql Workbench. The setting of my project is a university library where we have the tables: (Bold = PK | Italic = FK)
Emprestimo(ID_emprestimo,ID_funcionario, data_emprestimo)Emprestimo_Livro(ID_emprestimo,ID_livro, data_devolucao_livro)Livro(ID_livro, titulo, ISBN, formato, edicao, ano_da_edicao, paginas,ID_acervo,ID_editora)Emprestimo_Trabalho_Academico(ID_emprestimo,ID_trabalho_academico, data_devolucao_trabalho_academico)Trabalho_Academico(ID_trabalho_academico, titulo, ano_de_publicacao, matricula_professor,ID_acervo)Acervo(ID_acervo, campus)
I would like to select information from these tables: "All loans from a collection". These are the only tables that relate to the keys I need to reach the loans of materials belonging to a collection.
The table Emprestimo contains all loans made while the tables Emprestimo_Livro and Business & Workingacademic loans of their respective materials and the identity of these materials.
In turn, the tables Livro and Trabalho_Academico have the primary key of Acervo, which is the collection from where they belong. I was trying to select all the materials from a particular collection in this way (but without success):
SELECT Emprestimo.ID_emprestimo
FROM
Emprestimo
INNER JOIN
emprestimo_livro ON Emprestimo.ID_emprestimo = emprestimo_livro.ID_emprestimo
INNER JOIN
emprestimo_trabalho_academico ON Emprestimo.ID_emprestimo = emprestimo_trabalho_academico.ID_emprestimo
INNER JOIN
Livro ON Livro.ID_acervo = Acervo.ID_acervo
INNER JOIN
Trabalho_Academico ON Trabalho_Academico.ID_acervo = Acervo.ID_acervo
WHERE
Acervo.ID_acervo = 'C100';
Now I’m out of ideas, I don’t know how to select the book loans and academic works that belong to the same collection. Any help will be greatly appreciated! Thank you!
Go in parts: Consult all loans that contain a book that this book belongs to a collection. Sequence of Joins: Emprestimo -> emprestimo_livro -> livro -> acervo
WHERE Acervo.ID_acervo = 'C100';When you get this result,, make a similar consultation for academic work. Then make a union of the consultations through theUNION. The Biggest problem in your current query is that it is not merginginner joinwith the Collection table. Try a little more and report what you got.– Clarck Maciel