SELECT tbl01.* , 
       if('{ninvtype}' in ('1.6', '2.4', '5.1'), -1, 
       ifnull( (select mydt65_in.nr01
                from mydt65 as mydt65_in
                where mydt65_in.p01='{ninvtype}'
                   && if(mydt65_in.p03='*', '', mydt65_in.p03)=concat(tbl01.mydt57_p00, tbl01.mydt58_p00)
                   && if(mydt65_in.p04='*', '', mydt65_in.p04)=concat(tbl01.mydt61_p00, tbl01.mydt62_p00)
                limit 1
                ), 0) 
       ) as t_d65nr01   
FROM 
  ( {peg_str} 
  ) as tbl01;