How to delete a record without affecting the query?

1

I have two PRODUCT tables and CUSTOMERS. I am using inner join to link them in my queries.

I call the product and it displays the product, takes the customer from the CLIENTS table and displays it together in the query.

After deleting the client, the product is no longer displayed because the client no longer exists. How do I continue to view the product even without the customer?

    
asked by anonymous 20.11.2014 / 03:19

1 answer

2

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 the 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 the values that are present in the right table, 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 though they are 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.

    
20.11.2014 / 11:24