我正在尝试创建一个触发器,如果​​行的faction.factionname与INSERT的cards.faction匹配,则在将INSERT插入卡后增加派生表中的卡数。

到目前为止,我已经尝试了几种不同的方法,但是下面的代码是我最接近的猜测

create table cards(
    cardid int not null,
    faction varchar(255) not null,
    primary key(cardid)
);

create table faction(
    factionname varchar(255) not null,
    cardcount int not null,
    primary key(factionname)
);


create trigger updatecountinsert
    after insert on cards
    for each row
begin
    update faction
    set cardcount=carcount+1
    where last_insert_id().faction=faction.factionname
end;


基本上,如果我插入的值(11,'x')x是该派系,如果一行的派系具有factionname = x
触发应该
该行的“卡数”增加1

最佳答案

DELIMITER $$

CREATE TRIGGER updatecountinsert
AFTER INSERT ON cards
FOR EACH ROW
BEGIN
   -- NEW is used to qualify references values of the row that was just inserted
   -- e.g. NEW.faction is a reference to the value in the faction column
   UPDATE faction f
      SET f.cardcount = f.cardcount + 1
    WHERE f.factionname = NEW.faction ;
END;
$$

DELIMITER ;

关于mysql - 插入后触发到INCREMENT,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55403894/

10-09 20:50