How to delete a record without affecting the query?

Asked

Viewed 59 times

1

I have two tables PRODUCT and CUSTOMERS. I am using Inner Join to link them in my consultations.

I call the product and it displays the product, picks up the customer from the CUSTOMERS table and displays together in the query.

After deleting the customer, the product is no longer displayed as the customer no longer exists. How do I continue displaying the product even without the customer?

  • 4

    I believe that just changing the Inner Join by a Left Join should work.

  • Wakim’s probably right but we need to see how he’s doing query.

  • Post your query

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 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.