Update to one field taking into account two others from a different table

0

This is the code I am trying to use, with this achievement I make the Update to the field I want but only take into account one of the two fields I want to validate before the Update is made.

$sql = "UPDATE ingresos SET cod_factura='$fact' 
WHERE control = 
   (SELECT control 
   FROM (SELECT control 
      FROM ingresos, certificado 
      WHERE control='$con' AND cod_unico=unico_cod AND cod_certifica='$cod' )
      AS alias_ingresos )";
    
asked by Rsistemas 26.06.2018 в 14:08
source

1 answer

0

It would be something more or less of this style, in the absence of data I have assumed that the common field to both tables is 'control' and I have also assumed to which table the fields of the last line of conditions would belong, review it.

UPDATE i 
SET i.cod_factura='$fact'
FROM ingresos i 
INNER JOIN certificado c ON c.control = i.control
WHERE i.control='$con' 
AND c.cod_unico=i.unico_cod AND c.cod_certifica='$cod'

I hope that at least it will guide you.

Edit based on the sample comment:

UPDATE i 
    SET i.cod_factura='".$fact."'
    FROM ingresos i 
    INNER JOIN certificado c ON c.unico_cod = i.cod_unico
    WHERE i.control='".$con."' AND c.cod_certifica='".$cod."';
    
answered by 26.06.2018 в 15:11