Left Join repeat tables

1

I'm trying to mount a select that has the result below:

With the select below I can see null in only one column. How could I make it work for 20? I have item_1 through item_20 and the item description is in the parts table, in the occurrences it has only its code.

SELECT P1.descricao desc1
FROM ocorrencias
LEFT JOIN pecas P1
ON P1.cod_peccin=ocorrencias.item_1 
WHERE ocorrencias.cod = 2

I tried this way with two columns, but it did not work:

SELECT P1.descricao desc1, P2.descricao desc2
FROM ocorrencias
LEFT JOIN pecas P1, pecas P2
ON P1.cod_peccin=ocorrencias.item_1, P2.cod_peccin=ocorrencias.item_2
WHERE ocorrencias.cod = 2
    
asked by anonymous 07.03.2016 / 19:05

1 answer

2

To whom it may concern, I have decided as follows (here are 3 items):

SELECT P1.DESCRICAO AS desc1, P2.DESCRICAO AS desc2, P3.DESCRICAO AS desc3
FROM ocorrencias o LEFT JOIN
     pecas P1
     on P1.cod_peccin = o.item_1 LEFT JOIN
     pecas P2
     on P2.cod_peccin = o.item_2 LEFT JOIN
     pecas P3
     on P3.cod_peccin = o.item_3
where
     o.cod=2
    
07.03.2016 / 19:45