开发者

Trigger error due to INSERT INTO .. SELECT, error #1442

开发者 https://www.devze.com 2023-03-18 13:15 出处:网络
I need to do something similar to reputation/awards o开发者_如何学JAVAn stack overflow. Lets say I want to give out an award when a users rep exceeds 500. (award number 7 for example)

I need to do something similar to reputation/awards o开发者_如何学JAVAn stack overflow.

Lets say I want to give out an award when a users rep exceeds 500. (award number 7 for example)

But I only want the award to be given once, so if the rep decreased and then increase again, they wouldn't get two awards.

INSERT INTO awards (number, username, date) SELECT 7, username, NOW() FROM users WHERE rep>500;

And then have primary key username, number

Trigger:

CREATE TRIGGER `rewards` BEFORE INSERT ON `awards`
 FOR EACH ROW BEGIN
  UPDATE users SET points=points+1 WHERE username=NEW.username AND NEW.number=7;
END

but i get this error:

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


Simply create a trigger on the table awards

http://dev.mysql.com/doc/refman/5.1/en/create-trigger.html

0

精彩评论

暂无评论...
验证码 换一张
取 消