4
I have for example the following scenario:
Table Coluna01 Coluna02 Coluna03book id name gender
captl id name content
cplivro id idcap idlivro
When I have to associate a book with a chapter I use the table cplivro
putting the id
of the book and the id
of the chapter.
When for example I have the book with id 1
and I want to find his capitals I make 'two' SELECT
:
- First searching all records in
cplivro
where theidlivro
is equal to 1. - Second in a search in the captl table an element of the
array
resulting from the firstSELECT
Is it advantageous to do so? I tried to research a little and I think the UNION
I could use that, but I couldn’t understand, what it would be like with UNION
?
What you really want is to make a
JOIN
. I think your query would look something like this:SELECT 
 livro.*, cplivro.*, captl.*
FROM
 livro
 INNER JOIN
 cplivro ON (cplivro.idlivro = livro.id AND cplivro.idcap = captl.id);
– felipsmartins
Also, it seems to me unnecessary the existence of the table
cplivro
. I would move the fieldcplivro.idlivro
to the tablecaptl
.– felipsmartins
Guy the best is the
JOIN
, you can join the two tables by the common keys, in case the book id– KhaosDoctor