SELECT *
FROM 
(
select

   if({peg_arr[3]}.nr01 is NULL, 0, {peg_arr[3]}.nr01) as nr01
FROM a96
LEFT JOIN {peg_arr[3]} ON {peg_arr[3]}.nr01=a96.p04
WHERE a96.p01='{peg_arr[1]}'
  &&  a96.p02='{peg_arr[2]}'
  &&  a96.p03='{peg_arr[3]}'
  &&  a96.p05='{peg_arr[4]}' 
) as t1
WHERE t1.nr01<>0
LIMIT 1;
  
