【问题标题】:MySQL trigger to update value in another tableMySQL触发器更新另一个表中的值
【发布时间】:2011-03-23 13:00:21
【问题描述】:

我想要的是在更新/插入表 x 时,将表 y 中的值增加 1

下面的示例,使用了一个选择语句,我不能将其用作我正在尝试更新的表中选择

DELIMITER $$
create trigger occupancy
after insert on tbl_attendence
for each row
begin
    set @course_id = new.course_id;
    set @attendence_date = new.attendence_date;

    if new.reason = 1 then

        update tbl_course_occupancy
        set occupancy_number= (select occupancy_number 
                                from tbl_course_occupancy 
                               where course_id = @course_id 
                                 and occupancy_year = EXTRACT(year from @attendence_date) ) + 1
        where course_id = @course_id 
          and occupancy_year = Extract(year from @attendence_date);

    end if;

end$$

如有任何帮助,谢谢

【问题讨论】:

    标签: sql mysql triggers


    【解决方案1】:

    用途:

    UPDATE tbl_course_occupancy
       SET occupancy_number = occupancy_number + 1
     WHERE course_id = @course_id 
       AND occupancy_year = Extract(year from @attendence_date);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-15
      • 2014-10-20
      • 1970-01-01
      • 2020-11-10
      • 2022-07-08
      • 2012-06-23
      • 2021-03-04
      相关资源
      最近更新 更多