Mysql#1235错误代码,当创建具有不同目的的{2}触发器时

时间:2015-06-10 06:32:58

标签: mysql triggers

我有一个在phpmyadmin中创建的触发器,如:

BEGIN
UPDATE bands SET 
bands.status = "active"
where bands.id IN(
SELECT * FROM
(select bands.id 
from bands where bands.id IN 
(select bandId from invites join bands 
on(invites.bandId = bands.id) 
where invites.status="approved" 
group by bandId having count(invites.status)=bands.total_personel))AS X);
end

此触发器会将我的乐队状态更改为活动状态。

但是我想自动将状态更改为"不活动"如果它不符合where子句,也是如此。

如果我创建另一个触发器,如:

BEGIN
UPDATE bands SET 
bands.status = "inactive"
where bands.id IN(
SELECT * FROM
(select bands.id 
from bands where bands.id IN 
(select bandId from invites join bands 
on(invites.bandId < bands.id) 
where invites.status="approved" 
group by bandId having count(invites.status)=bands.total_personel))AS X);
end

Mysql给了我#1235错误代码。

如何解决此问题?或者有没有办法将这些目的结合在一个触发器中?

1 个答案:

答案 0 :(得分:0)

我找到了答案。我之前不知道UPDATE命令可以与WHEN条件结合使用。

这里是代码:

BEGIN
UPDATE bands SET status = CASE
WHEN bands.id IN
(SELECT * FROM
(select bands.id from bands where bands.id IN 
(select bandId from invites join bands 
 on(invites.bandId = bands.id) 
 where invites.status="approved" 
 group by bandId having count(invites.status)=bands.total_personel))AS X) 
 then "active"
 ELSE "inactive"
 END
 WHERE bands.id in (select distinct invites.bandId from invites);
 END

如果遇到when条件,Mysql代码会将状态更改为active,如果不满足WHEN条件,则会自动将状态更改为inactive。