This is the normal behavior of a INNER JOIN
it associates the records in one table with those in the other.
If in the contratos
table you have 1 record and in the borrowers 10 records linked to that contrato
, a inner join
between them will show 10 records repeating the fields that you placed referring to the contracts table
In your query, you are using an alias that has not been defined co.
in your tables from
In your case, you have INNER JOIN
between three tables, so the number of records will be the equivalence between them. In your specific case, if you have 1 contract, 3 os
of that contract and 3 mutuários
of this contract you should see 9 records. That is the combination of the three tables:
See this example: link
You will see that for each record of A
(in the example) the combination of B
and C
If you want only the contract to appear, without specifying any of the records that are duplicates, use the DISTINCT
clause in your query:
select DISTINCT
ct.id,
....
from
contratos ct
inner join
os ss on (ss.contratos_id=ct.id)
inner join
mutuarios mt on (mt.contratos_id=ct.id)
where....
It's important to remember that DISTINCT
will only delete duplicates for the scope of the select
columns, ie ct.id, ....