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

时间:2018-11-21 16:36:12

标签: php mysql sql triggers

我的jobPost表中有一个触发器,该触发器在更新列jobStatus和emailTeacher之后触发。触发器将插入所有与emailTeacher匹配的行,并将其插入到名为jobRequests的新表中。

我的问题是-更新一行时,具有该SAME 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;

END

Duplicate of entry

0 个答案:

没有答案