触发器在插入后在同一表中插入其他行

时间:2019-01-17 12:32:15

标签: mysql sql triggers

我正在使用Mysql服务器。我有一个包含订单的表(app_product_orders)和一个表,其中添加了带有注释的订单状态(app_product_order_states_details)。 在我的表app_product_orders中,每种颜色都有一行。 有时,评论可以是一种颜色,也可以是多种颜色(不仅是评论,在这种情况下,还可以设置交货日期)

我正在尝试创建一个触发器,例如“如果all_colors = 1,然后以相同的产品名称按此顺序为每行插入一行并将其链接到其顺序”,以便注释在每行中重复。

CREATE TRIGGER insert_app_product_order_states_details_date
AFTER INSERT ON app_product_order_states_details
FOR EACH ROW
BEGIN
IF (NEW.all_colors = 1 AND NEW.new_delivery_date is not null) THEN
INSERT INTO app_product_order_states_details 
SELECT
    concat(
    substring('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghaijklmnopqrstuvwxyz', rand()*(53-0)+0, 1),
    substring('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghaijklmnopqrstuvwxyz', rand()*(53-0)+0, 1),
    substring('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghaijklmnopqrstuvwxyz', rand()*(53-0)+0, 1),
    substring('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghaijklmnopqrstuvwxyz', rand()*(53-0)+0, 1),
    substring('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghaijklmnopqrstuvwxyz', rand()*(53-0)+0, 1),
    substring('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghaijklmnopqrstuvwxyz', rand()*(53-0)+0, 1),
    substring('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghaijklmnopqrstuvwxyz', rand()*(53-0)+0, 1),
    substring('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghaijklmnopqrstuvwxyz', rand()*(53-0)+0, 1)
        ),
    X.as_id_product_order,
    NEW.as_id_state,
    NEW.action,
    NEW.validate,
    NEW.comment,
    NEW.timestamp,
    NEW.as_id_factory,
    NEW.jiagongfei_factory,
    NEW.S,
    NEW.M,
    NEW.L,
    NEW.XL,
    NEW.SM,
    NEW.ML,
    NEW.TU,
    NEW.cost_price_pre_prod,
    NEW.jiagongfei_bangfang,
    NEW.jiagongfei_manager,
    NEW.jiagongfei_patricia,
    NEW.product_name,
    NEW.order_number,
    0,
    NEW.new_delivery_date
FROM app_product_orders X
WHERE X.product_name = NEW.product_name 
AND X.order_number = NEW.order_number
AND X.as_id_product_order != NEW.as_id_product_order;

END IF;

此触发器不起作用,一条消息提示它无法在同一表中插入行。 我对SQL还是很陌生,所以我想知道是否还有另一种方法可以使用其他方法来达到目标​​?

非常感谢!

0 个答案:

没有答案