How can I make a query that I add and subtract in a table

0
<?php
    $db = mysqli_connect("localhost", "root", "", "paypal");
    /* verificar la conexión */
    if (mysqli_connect_errno()) {
        die("Conexión fallida:" . mysqli_connect_error());
    }
    $sq = "INSERT INTO montos (usuario, monto) VALUES ('Andres', (SELECT SUM(monto ) montos FROM datos WHERE id IN (1,2)) )";    
    $result = mysqli_query($db, $sq);
    if ($result > 0 ) {
        $sql = mysqli_query($db,"UPDATE datos SET monto = monto - 50 WHERE id = 2 ");
    }
?>

I want to know if I can join the two queries to make a general query the first sum and the other makes a subtraction and updates the field that is being used.

    
asked by davidLDU 05.10.2018 в 16:42
source

1 answer

1

You can use this trigger for what you mention you need to do:

DELIMITER |

CREATE TRIGGER nombreTrigger BEFORE INSERT ON tuTabla
  FOR EACH ROW BEGIN
    //tu codigo 
    UPDATE datos SET monto = monto - 50 WHERE id = 2
  END
|

DELIMITER ;

Greetings.

    
answered by 06.10.2018 в 01:23