我有一个保存用户信息的用户表,每次用户看到一个页面时,它的 last_activity 列都会更新。
public function set_last_activity(){
global $db;
$t = time();
$sql = "UPDATE `users` SET `last_activity` = '$t'
WHERE
`id` = '$this->id' ";
$db->query($sql);
}
每个用户可以有 3 种类型 1 - 雇员或简称 ee 2 - 雇主或 er 3 - 两种类型
和状态字段是: 0:禁止,不活跃 1:活跃
现在我有触发器,它正在处理用户计数器
(ee_counter 用于雇员 [type:1,3] ,er_counter 用于雇主 [type:2,3])
在 stats 表中,当用户更改其状态时(0:禁止,1:活动),并且如果用户是雇员,它也会更新专业计数器
DELIMITER $$
CREATE TRIGGER user_status_change
AFTER UPDATE
ON users
FOR EACH ROW
BEGIN
UPDATE stats s
SET
ee_counter =
CASE
WHEN (NEW.type= 1 || NEW.type= 3 ) && OLD.status !=1 && NEW.status = 1 THEN ee_counter + 1
WHEN (NEW.type= 1 || NEW.type= 3 ) && OLD.status = 1 && NEW.status != 1 THEN ee_counter - 1
WHEN NEW.type= 3 && OLD.type = 2 THEN ee_counter + 1
WHEN OLD.type= 1 && NEW.type = 2 THEN ee_counter - 1
ELSE ee_counter
END ,
er_counter =
CASE
WHEN (NEW.type= 2 || NEW.type= 3 ) && OLD.status !=1 && NEW.status = 1 THEN er_counter + 1
WHEN (NEW.type= 2 || NEW.type= 3 ) && OLD.status = 1 && NEW.status != 1 THEN er_counter - 1
WHEN NEW.type= 3 && OLD.type = 1 THEN er_counter + 1
WHEN OLD.type= 2 && NEW.type = 1 THEN er_counter - 1
ELSE er_counter
END
WHERE
s.id = 1;
UPDATE specialitys s
JOIN speciality_objects o
ON s.id = o.speciality_id
JOIN users u
ON o.user_id = u.id
SET
counter =
CASE
WHEN NEW.status = 1 && OLD.status !=1 THEN counter + 1
WHEN NEW.status != 1 && OLD.status = 1 THEN counter - 1
ELSE counter
END
WHERE
o.user_id = NEW.id && s.id = o.speciality_id ;
END $$
DELIMITER ;
在我更换主机并购买了一个 vps 之前,它曾经工作正常
不,我不断收到此错误
Database query failed: Can't update table 'users' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
在我更新 last_activity 时的每一页上
我尝试使用 BEFORE UPDATE ,因为有些人建议它不起作用仍然出现此错误