DOUBT IN A SELECT - SQL

Asked

Viewed 41 times

0

I basically have these relationships: inserir a descrição da imagem aqui

I would like to take the value of the attribute "im_fab_template" of the table "tblImpressora" the code of the attribute "is_cod_printer" of the table "tblImpressoraSetor", someone can help me?

For now I have this select:

SELECT tblTonerEntSai.es_cod_toner_ent_sai, tblToner.tn_modelo, tblSetor.st_nome,

tblImpressoraSetor.is_cod_impressora, tblImpressoraSetor.is_siap, tblTonerEntSai.es_entrada_saida, 

tblTonerEntSai.es_quantidade, tblTonerEntSai.es_solicitante FROM tblTonerEntSai 

INNER JOIN tblToner ON (tblTonerEntSai.es_cod_toner = tblToner.tn_cod_toner)

INNER JOIN tblSetor ON (tblTonerEntSai.es_cod_setor = tblSetor.st_cod_setor)

INNER JOIN tblImpressoraSetor ON (tblTonerEntSai.es_cod_impressora_setor = tblImpressoraSetor.is_cod_imp_setor)

The same returns me the following data:

<table>
  <tr>
    <th>es_cod_toner_ent_sai</th>
    <th>tn_modelo</th>
    <th>st_nome</th>
    <th>is_cod_impressora</th>
    <th>is_siap</th>
    <th>es_entrada_saida</th>
    <th>es_quantidade</th>
    <th>es_solicitante</th>
  </tr>
  <tr>
    <td>1</td>
    <td>MODELO DA IMPRESSORA<br></td>
    <td>NOME DO SETOR<br></td>
    <td>3<br></td>
    <td>0000.0000<br></td>
    <td>09/07/2016<br></td>
    <td>4<br></td>
    <td>DEIVIDY<br></td>
  </tr>
</table>

Note: I don’t want to create a new relationship.

  • I think you could go into more detail, trying to understand here.

1 answer

2


If that’s what I understand, just do it:

SELECT tblImpressora.im_fab_modelo as Modelo
FROM tblImpressoraSetor
INNER JOIN tblImpressora ON (tblImpressora.im_cod_ = tblImpressoraSetor.is_cod_impressora);

Or:

SELECT tblImpressora.im_fab_modelo as Modelo
FROM tblImpressoraSetor, tblImpressora
WHERE tblImpressora.im_cod_impressora = tblImpressoraSetor.is_cod_impressora;

Obviously doing this by following the referential integrity constraints you must have done. From what I understand of your problem, I think this is it, very simple.


EDITION:
Got it, good you just add one more clause JOIN:

SELECT tblTonerEntSai.es_cod_toner_ent_sai, tblToner.tn_modelo, tblSetor.st_nome,
tblImpressoraSetor.is_cod_impressora, tblImpressoraSetor.is_siap, tblTonerEntSai.es_entrada_saida, 
tblTonerEntSai.es_quantidade, tblTonerEntSai.es_solicitante, tblImpressora.im_fab_modelo

FROM tblTonerEntSai 
    INNER JOIN tblToner ON (tblTonerEntSai.es_cod_toner = tblToner.tn_cod_toner)
    INNER JOIN tblSetor ON (tblTonerEntSai.es_cod_setor = tblSetor.st_cod_setor)
    INNER JOIN tblImpressoraSetor ON (tblTonerEntSai.es_cod_impressora_setor = tblImpressoraSetor.is_cod_imp_setor)
    INNER JOIN tblImpressora ON (tblImpressoraSetor.is_cod_impressora = tblImpressora.im_cod_impressora);

I think it works, you’re doing JOIN between tables then if I’m not mistaken I can reference tblImpressoraSetor.is_cod_impressora quietly that there is no problem, after all you are putting everything in the same table of result. The problem is if you can accept null values on some of the sides, then you would have to use LEFT JOIN or RIGHT JOIN, or other operations. But test there, should give.

[TIP]
Try to separate this query, it could be divided, it would be more readable and maybe even faster.

  • I added what you "were" wanting, is in quotes because I didn’t understand your problem right. Do the test there and see if this also works. I did the first answer that way because it gets much simpler and uncoupled things, I think better. The as Modelo is to rename the attribute tblImpressora.im_fab_modelo in the resulting table, which we agree, is much more readable.

Browser other questions tagged

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