简体   繁体   中英

Update all fields to journal table after each update

I have this trigger to update a single field in a journal table after each update, but I need to update ALL columns in related row in journal at once, not by naming each of them and separating by comma, how can I do that?

    DELIMITER //
    CREATE TRIGGER sfo_clone_update_subtotal_invoiced AFTER UPDATE ON sales_flat_order
    FOR EACH ROW BEGIN
        update sales_flat_order_journal set subtotal_invoiced=NEW.subtotal_invoiced where entity_id=new.entity_id;
    END;
DELIMITER ;

UPDATE syntax allows you to set multiple columns, separated by commas.

update sales_flat_order_journal 
set subtotal_invoiced = NEW.subtotal_invoiced,
    other_column1 = NEW.other_column,
    other_column2 = (/* constant expression, not based on NEW */),
    other_column3 = (SELECT ...scalar expression from some other tables... LIMIT 1),
    updated_at = NOW() 
where entity_id = new.entity_id;

You can access NEW.other_column to get the values of the same row that spawned the trigger.

You can DECLARE local variables in your trigger body to help calculate complex values.

You can use SELECT statements in your trigger body to query values from other rows or other tables, as long as you use a scalar SELECT that returns one row and one column.

If you need a more complex update that's too difficult to do in a trigger, I would do that in application code, not a trigger.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM