我有一个记录表:
mysql> desc MyLog;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| Aud_ID | int(11) | NO | PRI | NULL | auto_increment |
| Rec_ID | int(11) | NO | | NULL | |
| AudObj | varchar(255) | NO | | NULL | |
| User_ID | varchar(255) | NO | | NULL | |
| AudType | varchar(15) | NO | | NULL | |
| ChangedOn | datetime | YES | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
我有一个“插入后”的触发器:
CREATE TRIGGER after_Images_add
AFTER INSERT ON Images_TST
FOR EACH ROW
BEGIN
INSERT INTO AuditLog
SET Rec_ID = NEW.Image_ID,
AudType = 'ADDED',
AudObj = 'Images',
User_ID = NEW.employee_ID,
changedon = NOW();
END;
当我们向表中添加新记录时,在“插入后”,我的Rec_ID(它还不存在,直到被添加= 0),但在添加之后,我想要捕获“新” “Rec_ID并将其插入表中。我可以在同一个触发器中执行此操作吗?
这是我的输出:
mysql> select * from MyLog;
+----------+-----------+-------------+---------+-----------+---------------------+
| Aud_ID | Rec_ID | AudObj | User_ID | AudType | ChangedOn |
+----------+-----------+-------------+---------+-----------+---------------------+
| 1003 | 0 | Images | 445 | ADDED | 2014-10-28 09:09:42 |
+----------+-----------+-------------+---------+-----------+---------------------+
这是我要找的输出:
+----------+-----------+-------------+---------+-----------+---------------------+
| Aud_ID | Rec_ID | AudObj | User_ID | AudType | ChangedOn |
+----------+-----------+-------------+---------+-----------+---------------------+
| 1003 | 0 | Images | 445 | ADDED | 2014-10-28 09:09:42 |
| 1004 | 22| Images | 445 | NEWREC_ID | 2014-10-28 09:09:42 |
+----------+-----------+-------------+---------+-----------+---------------------+
任何建议将不胜感激。
答案 0 :(得分:1)
你应该能够按照自己的方式去做。只需使用INSERT INTO TABLE_NAME .. VALUES
构造
CREATE TRIGGER after_Images_add
AFTER INSERT ON Images_TST
FOR EACH ROW
BEGIN
INSERT INTO MyLog(Rec_ID, AudType, AudObj, User_ID, changedon)
VALUES(NEW.Image_ID, 'ADDED', 'Images', NEW.employee_ID, NOW());
END;