插入新表后如何避免重复行?-sql

ffscu2ro  于 2021-06-19  发布在  Mysql
关注(0)|答案(0)|浏览(243)

我的jobspost表中有一个触发器,它在更新jobstatus&emailteacher列后触发。触发器插入与emailteacher匹配的所有行,并插入到名为jobrequests的新表中。
我的问题是-当更新一行时,具有相同emailteacher的所有行都会插入到新表中。我只想插入已更新的受影响行。我在下面附上了sql。谢谢您。谢谢你的帮助。

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, lat, lng, 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.lat
    ,  jobPost.lng
    ,  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;

结束

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题