使用嵌套选择的MySQL触发器

时间:2012-03-12 23:36:57

标签: mysql sql database-trigger

我正在尝试创建一个MySQL触发器,它在表A上插入后运行,并且涉及多行。基本上,当我插入到表A中时,我想在表B中插入一行,其中包含表C中的每一行以及表A中插入的ID。有人可以帮忙吗?

这是一般的想法 -

DELIMITER |
CREATE TRIGGER mailings_create AFTER INSERT ON mailings FOR EACH ROW
BEGIN   
    SELECT
        customer_id,
        first_name,
        last_name,
        email
    INTO
        customer_id,
        first_name,
        last_name,
        email
    FROM
        cam_customers
    WHERE
        mail_list = 'T'
        AND email IS NOT NULL;

    foreach ($data as $row) {
        INSERT INTO
            mailings_messages
            (mailings_id, first_name, last_name, customer_id, email)
        VALUES
            (NEW.id, first_name, lsat_name, customer_id, email);
    }
...

谢谢!

埃文

1 个答案:

答案 0 :(得分:1)

怎么样 -

DELIMITER |

CREATE TRIGGER mailings_create AFTER INSERT ON mailings
    FOR EACH ROW BEGIN

        INSERT INTO mailings_messages
            (mailings_id, first_name, last_name, customer_id, email)
        SELECT NEW.id, first_name, last_name, customer_id, email
        FROM cam_customers
        WHERE mail_list = 'T'
        AND email IS NOT NULL;

        SELECT @row_count := ROW_COUNT();
        -- INSERT or UPDATE using @row_count here

    END;
|

DELIMITER ;