2017-04-14 67 views
4

我有兩個表:MySQL錯誤1442

sanction with these attributes: DriverID , Calification , Points
people with these attributes: pID , city , TotalPoints

,我有這個觸發器:

DROP TRIGGER IF EXISTS updatePoints_tgr; 

delimiter $$ 
CREATE TRIGGER 
updatePoints_tgr AFTER UPDATE 
ON sanctions FOR EACH ROW 
BEGIN 
    if NEW.points > OLD.points 
    THEN 
     UPDATE people 
     SET TotalPoints = TotalPoints + (NEW.points - OLD.points) 
     WHERE people.pID = NEW.DriverID; 
    elseif NEW.points < OLD.points 
    THEN 
     UPDATE people 
     SET TotalPoints = TotalPoints - (NEW.points - OLD.points) 
     WHERE people.pID = NEW.DriverID; 
    END IF; 
END$$ 
delimiter ; 

,當我嘗試執行此更新

UPDATE sanctions 
JOIN people ON sanctions.DriverID=people.pID 
SET points=points+6 
WHERE city='Barcelona' AND Calification='LightPenalty' 

我得到這個錯誤:

Can't update table 'people' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.

我該如何解決它?謝謝。

我使用的是Windows 10和MySQL服務器5.7.17

+0

我不是這方面的專家,但我不認爲你可以......對公平而言,對於'後期更新'有點令人驚訝。 –

+0

Windows 10 6.3.8版本構建1228 CE(64位) –

+0

我們可以試着證明(使用bug跟蹤器)它不是特定於該版本的bug(或者它可能是!),而是預期的行爲。稍後我會回到這個問題,看看我能找到什麼,如果沒有人打我的話,因爲它很有趣。 –

回答

0

不能更新,其中觸發器被調用的表(sanctionspeople):

Within a stored function or trigger, it is not permitted to modify a table that is already being used (for reading or writing) by the statement that invoked the function or trigger.

這樣做將產生錯誤1442:

Error Code: 1442 
Can't update table 'MyTable' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. 

你的情況下,:混亂可以是由於JOINsanctionspeopleUPDATE - 使得無論使用中的表中,如所述的上面的「使用」裝置「讀取或寫入」。


要測試 - 嘗試UPDATE查詢無JOIN(與people) - 如果它的工作原理,它肯定是這樣的。