Hello,
Whenever you want to return values that are present in both tables, use INNER JOIN
.
From tbesquerda inner join tbdireita
on tbesquerda.codigo = tbdireita.codigo
-------------------
|A.NOME | B.VALOR |
-------------------
1.|UM | 1.000 |
2.|UM | 2.000 |
3.|UM | 5.000 |
4.|DOIS | 4.000 |
5.|DOIS | 9.000 |
6.|TRES | 7.000 |
7.|CINCO | 4.000 |
-------------------
Whenever you want to return all values that are present in the table on the left, use LEFT JOIN
.
From tbesquerda left join tbdireita
on tbesquerda.codigo = tbdireita.codigo
-------------------
|A.NOME | B.VALOR |
-------------------
1.|UM | 1.000 |
2.|UM | 2.000 |
3.|UM | 5.000 |
4.|DOIS | 4.000 |
5.|DOIS | 9.000 |
6.|TRES | 7.000 |
7.|QUATRO | <NULL> |
8.|CINCO | 4.000 |
-------------------
Whenever you want to return all values that are present in the table on the right, use RIGHT JOIN
.
From tbesquerda right join tbdireita
on tbesquerda.codigo = tbdireita.codigo
-------------------
|A.NOME | B.VALOR |
-------------------
1.|UM | 1.000 |
2.|UM | 2.000 |
3.|UM | 5.000 |
4.|DOIS | 4.000 |
5.|DOIS | 9.000 |
6.|TRES | 7.000 |
7.|CINCO | 4.000 |
8.|<NULL> | 7.000 |
-------------------
Whenever you want to return all values that are in both tables even if it is null, use FULL OUTER JOIN
.
From tbesquerda full outer join tbdireita
on tbesquerda.codigo = tbdireita.codigo
-------------------
|A.NOME | B.VALOR |
-------------------
1.|UM | 1.000 |
2.|UM | 2.000 |
3.|UM | 5.000 |
4.|DOIS | 4.000 |
5.|DOIS | 9.000 |
6.|TRES | 7.000 |
7.|QUATRO | <NULL> |
8.|CINCO | 4.000 |
9.|<NULL> | 7.000 |
-------------------
Your example
Select tbproduto.produto,
tbcliente.cliente
From tbproduto left join tbcliente
on tbproduto.codigo = tbcliente.codigo
Now model the query for what you are looking for.
I believe that just changing the
Inner Join
by aLeft Join
should work.– Wakim
Wakim’s probably right but we need to see how he’s doing query.
– Maniero
Post your query
– LucasMotta