我有一个表访问者,其字段为Id,Name,MObile,EMail
&安培;带有字段Id,VisitorId [带访客表的ForeignKey],日期,目的的子表VisitorHistory
我正在针对VisitorHistory编写一个触发器,以便每个新插入都会记录到另一个表中 我使用下面的触发器
CREATE TRIGGER Trigger_VisitHistory_INSERT
AFTER INSERT
ON VisitorHistory FOR EACH ROW
BEGIN
DECLARE visitor_id integer;
DECLARE visitor_name varchar(255);
DECLARE priority integer;
DECLARE description varchar(550);
DECLARE activity varchar(255);
DECLARE actionId integer;
set @visitor_id=new.VisitorId;
set @visitor_name=''; Is any way to get visitor_name from the related table [foreign key table]
set @priority=1;
set @description= concat(' USer Visited again for ',new.Purpose);
set @activity='Revisit';
set @actionId=new.Id;
INSERT INTO ActivityLog (UserId,Priority,Activity,Description,ActionId) VALUES (@user_id,@priority,@activity,@description,@actionId);
END;
是否可以使用VisitorID [外键表]从相关表中获取visitor_name,以便我可以将其与日志中的描述一起使用
答案 0 :(得分:0)
我可以使用这种语法
INSERT INTO table (UserId, Priority, Activity, Description, ActionId, name)
SELECT @user_id,
@priority,
@activity,
@description,
@actionId,
user.name
FROM user
WHERE user.id = @user_id;
我没有对此进行测试,但是按照这个概念你应该让它工作