Query in two tables

Asked

Viewed 83 times

1

Well, how do I query two sql tables? For example, I have a table Enterprise with the fields id,social reason, and cnpj. I also have another table, Subsidiary with the fields id,addressee,loan. I want to select from the first table using id, cnpj and a select from the second table using only the address.

1 answer

5


Hello!

Just make a Join by the common key in both tables. Your code would look like this:

SELECT Empresa.id, Empresa.cnpj, Filial.endereco FROM Empresa INNER JOIN Filial on Empresa.id=Filial.empresa_id

In this case, you will only bring the results of the ids that are common in the Company and Affiliate table. If you want to return all Company table ids, replace INNER JOIN for LEFT JOIN.

Browser other questions tagged

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