使用SELECT / JOIN触发插入

时间:2014-10-20 08:22:23

标签: mysql sql

我在SELECTJOIN内写了一个触发器 INSERT AFTER:

DROP TRIGGER IF EXISTS `InsertArticle`;
DELIMITER //
CREATE TRIGGER `InsertArticle` AFTER INSERT ON `article`

FOR EACH ROW 
  insert into log (LogTime, LogIdNote, LogName, LogType, LogIdUser, logTypeCategory, LogTypeUser, LogUrl) 
    select NEW.ArticleTime, NEW.idArticle, NEW.ArticleName, 1, NEW.ArticleToUserID, NEW.ArticleCategory, u.UsersTypeAccount, ct.URLCategorysubscribetotype
    from users u where u.idUsers = NEW.ArticleToUserID LEFT JOIN categorysubscribetotype ct ON ct.CategoryTypeCategorysubscribetotype = 1;
END
//
DELIMITER ;

当我尝试创建触发器时,我得到错误1422 。 可能是我错误的触发sintax?

1 个答案:

答案 0 :(得分:1)

试试这个:

DROP TRIGGER IF EXISTS `InsertArticle`;
DELIMITER //
CREATE TRIGGER `InsertArticle` AFTER INSERT ON `article`

FOR EACH ROW 
  insert into log (LogTime, LogIdNote, LogName, LogType, LogIdUser, logTypeCategory, LogTypeUser, LogUrl) 
    select NEW.ArticleTime, NEW.idArticle, NEW.ArticleName, 1 as LogType, NEW.ArticleToUserID, NEW.ArticleCategory, u.UsersTypeAccount, ct.URLCategorysubscribetotype
    from users u  LEFT JOIN categorysubscribetotype ct ON ct.CategoryTypeCategorysubscribetotype = 1 where u.idUsers = NEW.ArticleToUserID;
END
//
DELIMITER ;