2

我正在尝试创建一个 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);
    }
...

谢谢!

埃文

4

1 回答 1

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 ;
于 2012-03-13T00:01:30.277 回答