gpt4 book ai didi

mysql - INSERT 后触发到 INCREMENT

转载 作者:行者123 更新时间:2023-11-30 21:32:44 29 4
gpt4 key购买 nike

如果行的faction.factionname 与 INSERT 的 cards.faction 匹配,我正在尝试创建一个触发器,在 INSERT into card 之后增加派系表中的 cardcount .

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

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;

基本上如果我插入 values(11,'x') x 是派系,如果一行派系有 factionname=x触发器应该将该行的“cardcount”增加 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 - INSERT 后触发到 INCREMENT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55403894/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com