Query in two tables

1

Well, how do I query on two tables in sql? For example, I have a Company table with the id , ravelosocial and cnpj fields. I also have another table, Branch with the fields id , address , company_id . I want to do a select of the first table using id, cnpj and a select from the second table using only the address.

    
asked by anonymous 01.06.2018 / 00:09

1 answer

5

Hello

Just do 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 get the results of the ids that are common in the Company and Branch table. If you want to return all the ids in the Company table, replace INNER JOIN with LEFT JOIN .

    
01.06.2018 / 00:42