DOUBT INTO A SELECT - SQL

0

I have basically this relationships:

Iwouldliketogetthevalueofthe"im_fab_model" attribute of the "tblPrinter" table from the code of the "is_cod_printer" attribute of the table "tblPrinterPrinter", can anyone 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 gives 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 do not want to create a new relationship.

    
asked by anonymous 09.07.2016 / 20:53

1 answer

2

If it's what I understood, it's only you to do:

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 following the referential integrity constraints you should have done. From what I understood of your problem, I think that's it, very simple.


EDITION:
I understand, it's only good that you add one more JOIN clause:

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 this works, you're doing JOIN between the tables so if I'm not mistaken I can reference tblImpressoraSetor.is_cod_impressora quietly that's fine, after all you're putting everything in the same result table . The problem is if you can accept null values on some sides, then you would have to use LEFT JOIN or RIGHT JOIN , or other operations. But do the test there, you must.

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

    
09.07.2016 / 21:18