当我的users_reputation
积分表更新(插入)时,我想对该用户的积分进行新计数(SUM)并更新表total_rep
中的一列users
。
这是我想出的,但不断收到语法错误。如果有人能指出我的错误,我将不胜感激。
CREATE TRIGGER after_insert_rep_points AFTER INSERT ON users_reputation
FOR EACH ROW
BEGIN
DECLARE new_total INT(10);
SELECT SUM(rep_points) INTO new_total FROM users_reputation
WHERE user_id = NEW.user_id;
UPDATE users SET rep_total = new_total WHERE user_id = NEW.user_id;
END
这些是我的表格示例供参考:
用户:
user_id | first_name | total_rep
-------------+----------------+--------------
10001 | Jim | 17
10002 | Bob | 5
USERS_REPUTATION:
user_id | rep_task | rep_points | rep_date
-----------+-------------------------------+--------------+-----------------------
10001 | Commented on article | 5 | 2012-11-12 08:40:32
10001 | Read an article | 2 | 2012-06-12 12:32:01
10001 | Shared an article | 10 | 2012-06-04 17:39:44
10001 | Read an article | 2 | 2012-05-19 01:04:11
10002 | Commented on article | 5 | 2012-06-17 09:34:21