Two Results in one query [closed]

0

I have the following example, two related tables

Tabela SETOR
SetorID [chave]
Setor
Vagas



Tabela FUNCIONARIOS
FuncionarioID[chave]
SetorID[chave estrangeira]
Nome
Funcao
CargaHoraria

I would like to do a SQL query filtering by Sector ID , where the result is as follows:

Display the data of the Sector and the employees linked to this sector.

    
asked by anonymous 28.11.2016 / 20:42

2 answers

2
SELECT * FROM SETOR NATURAL JOIN FUNCIONARIOS;

In this way, all employees will be shown with their sector on the side, if you need to filter the columns just change the * by the columns you need.

As you have the SetorID column in the two tables, you can use a NATURAL JOIN, which simplifies SQL but is the same as you did FUNCIONARIOS.SetorID = SETOR.SetorId

    
28.11.2016 / 20:49
-1
SELECT A.SETORID, SETOR, VAGAS, B.NOME FROM SETOR A 
INNER JOIN FUNCIONARIOS B ON A.SETORID=B.SETORID
WHERE A.SETORID = XXX

In this way, you join the two tables by SETORID and bring the information you need in a single query. Remembering that the INNER is used when we want to bring information that contains in both tables.

    
29.11.2016 / 19:07