简体   繁体   English

每次更新后,将所有字段更新到日记表

[英]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语法允许您设置多个列,并以逗号分隔。

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. 您可以访问NEW.other_column以获取生成触发器的同一行的值。

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. 您可以在触发器主体中使用SELECT语句从其他行或其他表中查询值,只要您使用返回一行和一列的标量SELECT

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. 如果您需要触发器中很难完成的更复杂的更新,则可以在应用程序代码中完成,而不是触发器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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