JOIN of 3 tables (LEFT JOIN)

0

I have a question regarding the best way (regarding performance and execution) to do the following select ... Initially I thought about using LEFT JOIN, but I came across some restrictions on its LEFT JOIN feature.

Having 3 following tables:

SERVICO
- S_ID
- S_CODIGO
- S_STATUS

ENTREGA
- E_ID
- E_CODIGO
- E_INFO
- D_FK_ID (FK com a tabela DIARIO, coluna D_ID)

DIARIO
- D_ID
- D_DATA

S_CODIGO (table SERVICO) and E_CODIGO (table DELIVERY) are uncommon and S_CODIGO has more data and the DELIVERY Table belongs to a part of these CODES (for that reason I thought of LEFT JOIN).

SELECT 
    s.S_CODIGO AS CODIGO,
    s.S_STATUS AS STATUS,
    e.E_INFO AS INFORMACAO,
    d.D_DATA_INSERT AS DATA_CADASTRO
FROM
    SERVICO s
LEFT JOIN
    ENTREGA e
    ON e.E_CODIGO  = s.S_CODIGO

From this select I wanted to do one more join to the DELIVERY table (column D_FK_ID) with the DAILY table (D_ID) to fetch the D_DATA_INSERT column.

to return something like this:

CODIGO - STATUS      - INFORMACAO - DATA_CADASTRO
200    - CONCLUIDO   - DIURNO...  - 2018-11-05
201    - ANDAMENTO   - DIURNO...  - 2018-11-05
203    - CONCUIDO    - DIURNO...  - 2018-11-05
205    - CONCUIDO    - null       - null
210    - CONCUIDO    - null       - null
    
asked by anonymous 06.11.2018 / 00:44

1 answer

1

Just add a new join and add the "DAILY" table:

SELECT 
    s.S_CODIGO AS CODIGO,
    s.S_STATUS AS STATUS,
    e.E_INFO AS INFORMACAO,
    d.D_DATA_INSERT AS DATA_CADASTRO
FROM
    SERVICO s
LEFT JOIN
    ENTREGA e
    ON e.E_CODIGO  = s.S_CODIGO
LEFT JOIN DIARIO d
    ON e.D_FK_ID = d.D_ID

You can add as many% as required. I copied his example and made a join , but could be any other type of LEFT JOIN .

Here's another similar question: SQL JOIN Multiple Tables

    
06.11.2018 / 02:06