2011-05-17 90 views
1

我在我的mysql數據庫中有兩個表: 顧問:編號 學生:advisorID |主要爲什麼這個觸發器不按我想要的方式工作?

我想更新學生,其中Advisor中刪除的ID等於advisorID並使用一組if語句來根據該學生的專業更改該學生的advisorID值。這是我迄今爲止:

delimiter // 
    create trigger advisor_delete after delete 
    on advisor 
     UPDATE Student 
      IF Student.major = 'major1' THEN SET Student.advisorID = 1; 
      ELSEIF Student.major = 'major2' THEN SET Student.advisorID = 2; 
      ELSEIF Student.major = 'major3' THEN SET Student.advisorID = 3; 
      ELSE SET Student.advisorID = 4; 
      ENDIF; 
     WHERE Student.advisorID = OLD.id; 
    end// 

任何幫助是值得歡迎的。謝謝。

回答

1

我不知道這UPDATE/IFELSEIF語法,但CASE聲明會爲你工作:

delimiter // 
create trigger advisor_delete after delete 
on advisor 
    UPDATE Student 
     SET Student.advisorID = 
      CASE Student.major WHEN 'major1' THEN 1 
           WHEN 'major2' THEN 2 
           WHEN 'major3' THEN 3 
           ELSE 4 
      END 
    WHERE Student.advisorID = OLD.id; 
end// 
相關問題