如何使用条件和左连接创建MYSQL触发器?

时间:2016-10-17 20:13:47

标签: php mysql database triggers

所以我有一个我一直在寻找繁琐的场景,但找不到两者都出现的情况。

我有一个我想要的数据库,如果更新了特定的表,它会执行触发器来查看该表的artjobs_production和第二个表artsjobs中的值。我不同意在我加入之前数据结构的形成方式;但是我想在不重构大量代码的情况下解决这个问题。

想法是一旦满足某些条件我想要触发并更新每个特定行更新的日期。

这是我创建的基本触发器,如果​​可能的话,我想知道正确的语法将其设置为触发器?

CREATE TRIGGER createProductionScheduleDate
AFTER UPDATE ON artworkjobs_production
FOR EACH ROW
  IF (((OLD.artworkjobs.deleted = 0) OR (NEW.artworkjobs.deleted = 0))
      AND ((OLD.Status = 'Approved' OR OLD.scheduleForProd = 1) OR (NEW.Status = 'Approved' OR NEW.scheduleForProd = 1))
      AND ((OLD.prod.isCompleted IS NULL OR OLD.prod.isCompleted = 'N') OR
           (NEW.prod.isCompleted IS NULL OR NEW.prod.isCompleted = 'N'))
      AND ((OLD.printDueDate <> '0000-00-00 00:00:00') OR (NEW.printDueDate <> '0000-00-00 00:00:00'))
      AND (length(OLD.invoiceNumber) > 0) OR (length(NEW.invoiceNumber) > 0))
     AND ((OLD.importance > 0) OR (NEW.importance > 0))
     AND ((OLD.quantity > 0) OR (NEW.quantity > 0))) THEN
UPDATE artworkjobs_production
SET originalProdScheduleDate = now();
WHERE ON LEFT JOIN artworkjobs_production prod ON prod.jobid = id JOIN tblaccounts ON acc_id = CustomerID;
END$$

注意:我玩了一下并且稍微更新了我的语法,但是接近结尾时出现语法不正确的错误。

这是新代码:

CREATE TRIGGER createProductionScheduleDate
AFTER UPDATE ON artworkjobs_production
FOR EACH ROW
BEGIN
UPDATE artworkjobs_production
LEFT JOIN artworkjobs artworkjobs
ON jobid = artworkjobs.id
SET originalProdScheduleDate = now()
WHERE (((OLD.artworkjobs.deleted = 0) OR (NEW.artworkjobs.deleted = 0))
        AND ((OLD.artworkjobs.Status = 'Approved' OR OLD.artworkjobs.scheduleForProd=1 ) OR (NEW.artworkjobs.Status = 'Approved' OR NEW.artworkjobs.scheduleForProd=1 ))
        AND ((OLD.isCompleted is Null OR OLD.isCompleted = 'N') OR (NEW.isCompleted is Null OR NEW.isCompleted = 'N'))
        AND ((OLD.printDueDate <> '0000-00-00 00:00:00') OR (NEW.printDueDate <> '0000-00-00 00:00:00'))
        AND (length(OLD.invoiceNumber) > 0) OR (length(NEW.invoiceNumber) > 0)) 
        AND ((OLD.importance > 0) OR (NEW.importance > 0))
        AND ((OLD.quantity > 0) OR (NEW.quantity > 0)))
END;

0 个答案:

没有答案