多个 GROUP_CONCAT 语句 INTO MYSQL 触发器

Multiple GROUP_CONCAT Statement INTO MYSQL Trigger

DELIMITER |

DROP TRIGGER IF EXISTS update_trigger |

CREATE TRIGGER update_trigger
   BEFORE UPDATE ON suborder
   FOR EACH ROW
   BEGIN
   DECLARE change_value TEXT;
   DECLARE new_data TEXT;
   DECLARE changed_val BOOL;
   DECLARE data TEXT;
   DECLARE data2 TEXT;
   SET data = '';
   SET new_data = CONCAT("{", "\"order_id\"", ":", "\"", NEW.order_id, "\"");
   SET changed_val = FALSE;

   IF( NEW.order_status_id > 0 ) THEN

    IF ( OLD.suborder_id != NEW.suborder_id ) THEN
        SET changed_val = TRUE;

        SELECT GROUP_CONCAT( suborder_id SEPARATOR ',' ) INTO data,
               GROUP_CONCAT( suborder_id SEPARATOR ',' ) INTO data2
        FROM oc_suborder
        WHERE order_id = NEW.order_id AND
              suborder_id != OLD.suborder_id
        GROUP BY order_id;

        SET new_data := CONCAT(new_data, ",", "\"suborders\"", ":{");
        SET new_data := CONCAT(new_data, "\"old\"", ":", "\"",data ,",", OLD.suborder_id, "\"");
        SET new_data := CONCAT(new_data, ",\"new\"", ":", "\"",data ,",", NEW.suborder_id, "\"");
        SET new_data := CONCAT(new_data, "}");
    END IF;
    SET new_data := CONCAT(new_data, "}");



    IF (changed_val = TRUE) THEN
        INSERT INTO oc_order_change_log
        SET order_id = NEW.order_id,
            table_name = 'oc_suborder',
            change_details = new_data,
            date_added = NOW(),
            solr_synced = 0;
    END IF;

    END IF;
    END |
   DELIMITER ;

显示为错误:

1064 - 你的 SQL 语法有误;查看与您的 MySQL 服务器版本对应的手册,了解在 'GROUP_CONCAT( invoice_no SEPARATOR ',' ) INTO data2

附近使用的正确语法

我不是 MySQL 触发器的专家,但我相信 SELECT ... INTO 语法应该类似于 INSERT,即

SELECT col1, col2 INTO var1, var2

试试这个代码:

IF ( OLD.suborder_id != NEW.suborder_id ) THEN
    SET changed_val = TRUE;

    SELECT GROUP_CONCAT(suborder_id SEPARATOR ','),
           GROUP_CONCAT(suborder_id SEPARATOR ',')
    INTO data, data2
    FROM oc_suborder
    WHERE order_id = NEW.order_id AND
          suborder_id != OLD.suborder_id
    GROUP BY order_id;

    SET new_data := CONCAT(new_data, ",", "\"suborders\"", ":{");
    SET new_data := CONCAT(new_data, "\"old\"", ":", "\"",data ,",", OLD.suborder_id, "\"");
    SET new_data := CONCAT(new_data, ",\"new\"", ":", "\"",data ,",", NEW.suborder_id, "\"");
    SET new_data := CONCAT(new_data, "}");
END IF;

你有语法问题

SELECT GROUP_CONCAT( suborder_id SEPARATOR ',' ) INTO data,
               GROUP_CONCAT( suborder_id SEPARATOR ',' ) INTO data2
        FROM oc_suborder
        WHERE order_id = NEW.order_id AND
              suborder_id != OLD.suborder_id
        GROUP BY order_id;

替换为

SELECT GROUP_CONCAT( suborder_id SEPARATOR ',' ) ,
       GROUP_CONCAT( suborder_id SEPARATOR ',' ) INTO data,data2
       FROM oc_suborder
       WHERE order_id = NEW.order_id AND
              suborder_id != OLD.suborder_id
       GROUP BY order_id;