表中更新后的内部连接

vptzau2j  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(342)

在mysql数据库中有一个名为jobsposts的表。我试图在当前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表

jv4diomz

jv4diomz1#

从评论来看,似乎你只是需要一个 Inner JoinjobPost 以及 users table,开着 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 ;

相关问题