MYSQL-表中的AFTER UPDATE进行内部联接

时间:2018-10-29 18:25:17

标签: mysql

我的MySQL数据库中有一个名为jobPosts的表。我试图在当前jobPost表中发生更新后将数据插入到另一个表(jobRequest)中。

这是我的SQL表和查询。

           DELIMITER //
           CREATE DEFINER=`root`@`localhost` TRIGGER 
           trg_jobPost_after_update
           AFTER UPDATE ON jobPost
           FOR EACH ROW
           BEGIN
           INSERT INTO jobRequest (userID, name, email, phoneNo, 
            location, jobID, 
                   title, level, dateFrom, dateTo, description, 
                   jobStatus, emailTeacher, nameTeacher, 
                   locationTeacher, phoneNoTeacher, cv, gardavetting, 
                   linkedin) 
      SELECT 
        jobPost.userID
      ,  jobPost.name
      ,  jobPost.email
      ,  jobPost.phoneNo
      ,  jobPost.location
      ,  jobPost.jobID
      ,  jobPost.title
      ,  jobPost.level
      ,  jobPost.dateFrom
      ,  jobPost.dateTo
      ,  jobPost.description
      , users.nameTeacher
      , users.locationTeacher
      , users.phoneNoTeacher
      , users.cv
      , users.gardavetting
      , users.linkedin
      , NEW.jobStatus
     , NEW.emailTeacher
   FROM jobPost
   WHERE jobPosts.emailTeacher = NEW.emailTeacher;
  END //
  DELIMITER ;

表格-

jobPost表

用户表

jobRequest Table

1 个答案:

答案 0 :(得分:0)

从评论看来,您似乎只需要Inner JoinjobPostusers表之间的emailTeacher

Select子句相比,Insert的某些列也丢失了;我已经按照正确的顺序添加了它们。

DELIMITER //
CREATE DEFINER=`root`@`localhost` TRIGGER 
    trg_jobPost_after_update
    AFTER UPDATE ON jobPost
       FOR EACH ROW
       BEGIN

       -- Check if jobStatus is Updated (then only we Insert) 
       IF ( OLD.jobStatus <> NEW.jobStatus AND 
            OLD.emailTeacher <> NEW.emailTeacher ) THEN 
         INSERT INTO jobRequest (userID, name, email, phoneNo, 
                                 location, jobID, title, level, 
                                 dateFrom, dateTo, description, 
                                 jobStatus, emailTeacher, nameTeacher, 
                                 locationTeacher, phoneNoTeacher, cv, 
                                 gardavetting, linkedin) 
         SELECT 
           jobPost.userID
        ,  jobPost.name
        ,  jobPost.email
        ,  jobPost.phoneNo
        ,  jobPost.location
        ,  jobPost.jobID
        ,  jobPost.title
        ,  jobPost.level
        ,  jobPost.dateFrom
        ,  jobPost.dateTo
        ,  jobPost.description
        ,  NEW.jobStatus 
        ,  NEW.emailTeacher 
        ,  users.nameTeacher 
        , users.locationTeacher
        , users.phoneNoTeacher
        , users.cv
        , users.gardavetting
        , users.linkedin
        FROM jobPost 
        JOIN users ON users.emailTeacher = jobPost.emailTeacher 
        WHERE jobPost.emailTeacher = NEW.emailTeacher;

      END IF;
END //
DELIMITER ;