Mysql SQL';插入触发器后';投诉未知列';currentId';在字段列表中,它是游标
我有两张桌子Mysql SQL';插入触发器后';投诉未知列';currentId';在字段列表中,它是游标,mysql,sql,Mysql,Sql,我有两张桌子 create table comments ( id int auto_increment primary key, task_id int not null, foreign key fk_comment_task(task_id) references tasks(id), user int not null, foreign key fk_comment_user(user) references users(id), comm
create table comments (
id int auto_increment primary key,
task_id int not null,
foreign key fk_comment_task(task_id) references tasks(id),
user int not null,
foreign key fk_comment_user(user) references users(id),
comment varchar(1000) not null,
comment_date date not null,
viewed tinyint(1) not null default 0,
deleted tinyint(1) not null default 0
) engine = InnoDB;
第二个:
create table viewed_comments (
id int auto_increment primary key,
comment int not null,
foreign key fk_viewed_comment(comment) references comments(id),
viewer int not null,
foreign key fk_viewed_viewer(viewer) references users(id),
unread tinyint(1) not null default 0,
deleted tinyint(1) not null default 0
) engine = InnoDB;
我想创建一个触发器,当注释插入“comments”表时,“viewsed_comments”表为所有用户(提交注释的用户除外)创建该注释的行
目前,我有一个触发器:
delimiter |
create trigger ins_views after insert on comments
for each row
begin
DECLARE finished INT DEFAULT 0;
DECLARE id INT DEFAULT 0;
DECLARE currentId CURSOR FOR SELECT id FROM users WHERE id != NEW.user;
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET finished = 1;
OPEN currentId;
update_views_loop: LOOP
FETCH currentId INTO id;
IF finished = 1 THEN LEAVE update_views_loop;
END IF;
INSERT INTO viewed_comments (comment, viewer) VALUES (NEW.id, currentId);
END LOOP update_views_loop;
CLOSE currentId;
END;
|
delimiter ;
当我插入诸如“插入注释(任务id、用户、注释、注释日期)”的值(24、4、“测试”、“2018-3-5”)时;作为一个例子,我得到了以下响应。
错误1054(42S22):光标“字段列表”中的未知列“currentId”。我该如何解决这个问题
INSERT INTO viewed_comments (comment, viewer) VALUES (NEW.id, id);
因为您将值读入变量id。请使用此值,而不是光标
FETCH currentId INTO id;