触发器创建查询失败

时间:2014-03-05 15:36:26

标签: mysql

我有三个表,当数据插入第三个表时,我检查新插入的行是否在table1中有字段值。如果值存在,那么我将同一行从table3移动到table2。我的代码如下所示,但是在执行此代码时出现错误

DELIMITER $$
CREATE TRIGGER onRosterAdd
 AFTER INSERT ON yet_tobe
 FOR EACH ROW 
   BEGIN
    SELECT CASE WHEN (SELECT 1 FROM users WHERE NEW.jid = (users.username + "_1")) > 0 
     THEN 
       INSERT INTO rusers SELECT * FROM yet_tobe WHERE yet_tobe.id = NEW.id;
       DELETE FROM yet_tobe where yet_tobe.id = NEW.id;
     END        
   END$$
DELIMITER ;

这是错误

您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在'INTO rusers SELECT * FROM yet_tobe WHERE yet_tobe.id = NEW.id;

附近使用正确的语法。

我该如何解决这个问题。

1 个答案:

答案 0 :(得分:1)

试试这样:

DELIMITER $$
CREATE TRIGGER onRosterAdd
BEFORE INSERT ON yet_tobe
FOR EACH ROW 
BEGIN
   IF (SELECT 1 FROM users WHERE CONCAT(username,"_1")=NEW.jid)>0
   THEN 
      INSERT INTO rusers SELECT * FROM yet_tobe WHERE yet_tobe.id = NEW.id;
      DELETE FROM yet_tobe where yet_tobe.id = NEW.id;
   END IF;
 END$$
 DELIMITER ;

这是一个有效的fiddle