MySQL错误1442

时间:2017-04-14 16:43:13

标签: mysql

我有两张桌子:

  

sanction具有以下属性:DriverIDCalificationPoints
  people具有以下属性:pIDcityTotalPoints

我有这个触发器:

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'

我收到此错误:

  

无法更新表格&#39; people&#39;在存储的函数/触发器中因为它   已经被调用此存储函数/触发器的语句使用。

我该如何解决?感谢。

我使用Windows 10和MySQL服务器5.7.17

1 个答案:

答案 0 :(得分:0)

您无法更新调用触发器的表(sanctionspeople):

  

在存储的函数或触发器中,不允许修改a   已经被使用(用于读或写)的表   调用函数或触发器的语句。

这样做会产生错误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.

您的案例:混淆可能是由JOIN上的sanctionspeople上的UPDATE引起的。使用,如上所述' used '表示'阅读或写作'。

进行测试 - 尝试UPDATE JOIN查询peopleselect b.sitecatid,d.locationid from (SELECT @rownum := @rownum + 1 AS row_number, a.sitecatid FROM (SELECT DISTINCT sitecatid FROM tbl_test) a , (SELECT @rownum := 0) r) as b right join (SELECT @rownum_2 := @rownum_2 + 1 AS row_number, c.locationid FROM (SELECT DISTINCT locationid FROM tbl_test) c , (SELECT @rownum_2 := 0) s) as d on b.row_number = d.row_number ) - 如果有效,肯定是这样的。