Select with 3 tables in SQL SERVER

Asked

Viewed 2,042 times

2

Tabela de Maquina

Tabela Equipamento

Tabela de Marca

I need to make a select in the machine table, but when I run my command

select maq.maqNip, equi.* from tblMaquina maq
join tblEquipamento equi on equi.equId = maq.maqTipoEqui;

the following result comes outResultado do select and I wanted the brand name to appear, the model and not just your ID.

Could someone help me link these three tables?

  • Just make one more Join with the Tag table through Doz Equipment attributes.Brand = Brand.ID, which seem to have dieferent names on their real tables.

4 answers

5


Just add one more INNER JOIN with the other table of Marca:

SELECT * 
  FROM tblMaquina maq
 INNER JOIN tblEquipamento equi 
    ON equi.equId = maq.maqTipoEqui;
 INNER JOIN tblMarca marca
    ON equi.marcaId = marca.marcaId;

Observing: Don’t forget to change table and field names to match your template. I did this as an example, because I do not have the names of their fields.

3

Try to do so:

select maq.maqNip, equi.*, marc.* from tblMaquina maq
inner join tblEquipamento equi on equi.equId = maq.maqTipoEqui
inner join tblMarca marc on marc.marcId = equi.equMarca

0

Try to run the following query

select 
   maq.Nip, 
   equip.tipo, 
   marc.nome 
from 
   Maquina maq,
   Marca marc,
   Equipamento equip,
where
   maq.tipo = equip.id
and
   equip.marca = marc.id;

0

Also includes a (NOLOCK) not to allocate the tables in processing time

SELECT * 
  FROM tblMaquina (NOLOCK) maq
 INNER JOIN tblEquipamento (NOLOCK) equi 
    ON equi.equId = maq.maqTipoEqui;
 INNER JOIN tblMarca (NOLOCK) marca 
    ON equi.marcaId = marca.marcaId;
  • 1

    The NOLOCK will make a dirty read, IE, if he is in a transaction that has not yet been "committed", will read the old data. In question he did not say if you need this situation.

Browser other questions tagged

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