触发器内的MYSQL SELECT INSERT

时间:2015-08-13 18:40:28

标签: mysql select triggers insert

    delimiter //
CREATE TRIGGER trig AFTER INSERT ON table0 FOR EACH ROW 
BEGIN
    CREATE TEMPORARY TABLE temp (SELECT t1.col1,t1.Col2,t2.col3,t3.col4,t4.col5 FROM (table1 AS t1 JOIN table2 AS t2 ON t1.col1 = t2.col3... etc) WHERE t1.condition = NEW.condition);
    INSERT INTO table5 VALUES (NULL,CURRENT_TIMESTAMP(),0,0,NULL,NULL,NULL,NEW.condition,   SELECT GROUP_CONCAT("\"",someColumn - 1,",",50,",",someColumn2,",",30,",",someColumn3,"\"" SEPARATOR ';') FROM temp);
    DROP temp;
END;//
delimiter ;

错误现在是

错误1064(42000):您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在#SELECT; SELECT CONCAT附近使用正确的语法(" \"",someColumn - 1,",&#34 ;,50,",",someColumn2,",",30,",",someColu'在第4行

它在someColumn3的someColu被切断了

感谢您的时间。

1 个答案:

答案 0 :(得分:1)

你似乎错过了行分隔符。你需要添加它们。

delimiter //
CREATE TRIGGER upd_check BEFORE UPDATE ON account
FOR EACH ROW
BEGIN
    IF NEW.amount < 0 THEN
        SET NEW.amount = 0;
    ELSEIF NEW.amount > 100 THEN
        SET NEW.amount = 100;
    END IF;
END;//
delimiter ;

必须将分隔符更改为“;”以外的其他内容这样执行就不会在create trigger语句中间终止。

这里我们在创建触发器之前将分隔符更改为“//”并将其更改回“;”立即