how to populate mysql column value based on a formula?

前端 未结 2 1566
礼貌的吻别
礼貌的吻别 2021-01-06 15:01

I have created a mysql table with the following columns...

item_price, discount, delivery_charge, grand_total

The value of item_price

2条回答
  •  粉色の甜心
    2021-01-06 15:12

    I think you'd have to use a BEFORE INSERT trigger for that:

    http://dev.mysql.com/doc/refman/5.6/en/create-trigger.html

    Something like this (untested off the top of my header code):

    CREATE TRIGGER blahblah BEFORE INSERT ON your_table
    FOR EACH ROW BEGIN
        set new.grand_total = new.item_price - new.discount + new.delivery_charge;
    END;
    

提交回复
热议问题