sql >> Base de Datos >  >> RDS >> Mysql

SQL Cómo sumar de otra tabla e insertar en otra tabla

Agregue este disparador a su ExpenseTable

CREATE TRIGGER ExpenseSum AFTER INSERT ON ExpenseTable FOR EACH ROW
BEGIN
    UPDATE ProjectsTable P
    SET ExpenseTotal = 
    (SELECT SUM(ExpenseAmount) from ExpenseTable
    where ExpenseTable.ProjectID= P.ProjectID)
    where P.ProjectID = New.ProjectID;
END

No olvides agregar el disparador After Update y After Delete para actualizar ExpenseTotal