Query relationship too much for many SQL server

3

Hello, I have a question to make a select on SQL SERVER . I have 3 tables. They are, BOOKS, AUTHOR and BOOK_AUTOR . A BOOK_AUTOR has a foreign key of BOOKS AND AUTHOR . I need to make a id that returns all books with authors born in Brazil (this field has in the author table). I've been thinking about this problem for some time, it seems simple. but so far I have not found a way to make it work.

    
asked by anonymous 21.03.2016 / 04:44

2 answers

2

Jhow has more than one way to solve your problem, one of them would be:

      select titulo from livros inner join livro_autor on livros.id = livro_autor.fk_livro 
      inner join autor on autor.id = livro_autor.fk_autor 
      where autor.pais = 'Brasil'group by livros.id , livros.titulo 
    
21.03.2016 / 05:05
2

Hello, I do not know the structure of your tables, so I'll try to help you ...

select livros.titulo,
       autor.pais
From livros inner join livro_autor
On livros.objectid = livro_autor.objectidlivro
Inner join autor
On autor.objectid = livro_autor.objectidautor
where autor.pais = 'Brasil'

livros.titulo should be replaced with the name of the column in which the title information in your books table is. (% with%)

Livros.nome_da_coluna should be replaced with the name of the column where the country information is in the author table. (% with%)

In the inner join, where autor.pais is to be replaced by the name of the column where the information that is related in your tables is located, initially in the books table, then in the Autor.nome_da_coluna that relates to books. Then the author table column that relates to the objectid table.

I hope I have helped you, if you have been a bit confused, please inform the names of these columns that I re-assemble the correct script.

Please let me know if it works.

    
21.03.2016 / 05:31