【问题标题】:Can mysql trigger update a cell?mysql触发器可以更新单元格吗?
【发布时间】:2013-10-19 04:39:53
【问题描述】:

我有一个 mysql 表“市场”,其中包含 open_time (datetime)session_num (int)duration (int)close_time (datetime) 列。

如果有人插入 [open_time, session_num, duration],如何触发自动计算 close_time 的默认值?

我想要close_time = open_time + session_num * duration

触发器可以做到吗?谢谢。

【问题讨论】:

    标签: mysql triggers


    【解决方案1】:

    是的,您可以为插入创建触发器,如下所示:

    delimiter ;;
    create trigger insertTriggerTest
    before insert on sampletable
    for each row
    begin
        set new.close_time = new.open_time + new.session_num * new.duration;
    end;;
    delimiter ;
    

    注意 - new.session_num * new.duration 应该以秒为单位

    【讨论】:

      【解决方案2】:

      当然可以,您只需要根据插入的行更新值。

      这里是how you can do this

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-18
        • 2010-12-23
        • 1970-01-01
        • 2015-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-11
        相关资源
        最近更新 更多