mysql - SQL“插入触发器后”抱怨字段列表中的未知列“currentId”,即游标
问题描述
我有 2 张桌子。
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;
我想创建一个触发器,当评论插入到“评论”表中时,“viewed_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 ;
当我插入诸如“插入评论(task_id、user、comment、comment_date)值(24、4、'test'、'2018-3-5')之类的内容时;例如,我得到以下响应。错误 1054 (42S22):“字段列表”中的未知列“currentId”是光标。我该如何解决?
解决方案
INSERT INTO viewed_comments (comment, viewer) VALUES (NEW.id, id);
因为您将值读入变量 id。使用此值而不是光标。
FETCH currentId INTO id;
推荐阅读
- php - 如果在 php 中以特殊字符 (=) 开头,则不要替换关键字
- angular - 基于导航,在 Angular 中显示/隐藏一些控件
- nginx - 无法使用 lua nginx 配置 OIDC
- php - 我正在修改我们学校的酒店预订系统,我的问题是我很难显示我的价格输出
- python - 如何将 Final_Unique 列转换为 pandas 中的标题值
- javascript - JavaScript Promise 返回 Pending 但我只需要该值
- php - 将父查询复制到另一个变量中并向其添加更多条件
- powershell - 当我使用命令行使用 Text-to-Speech 创建合成语音音频时,如何将 Powershell 的输出保存到文件中
- c# - System.Data.SqlClient.SqlException:'9'附近的语法不正确
- sql-server - 创建一个动态 SQL Where 子句,将 where 语句的两个部分都传入参数?即其中变量 1 = 变量 2