简体   繁体   中英

MySQL Trigger - Compare Values of two different tables

Im currently struggling with the implementation of a trigger in MySQL. My scenario is the following:

There are two databases ( emailregistration & answers ) with a different set of ids as the primary key. Therefore I want to identify the correct row that needs to be updated using the email adress as the identifier.(which is identical in both tables) I want to start a trigger, when the database "registration" is updated. This trigger then updates a value in "users". (represented as confirmed/unsubscribed )

The problem from my understanding is, that there is no check for the correct row. My code below is taking directly form phpmyadmin. In its current form the statement fills up the answers database with empty rows and changes the column confirmed/unsubscribed as desired but its not identifying the right row.

    AFTER
    UPDATE
    ------
    INSERT INTO answers (confirmed, unsubscribed)
    SELECT emailregistration.confirmed, emailregistration.unsubscribed
    FROM emailregistration
    WHERE emailregistration.email = email
    -----
    root@localhost

    *This should translate into the following trigger code:*

    CREATE TRIGGER DOI 
    AFTER UPDATE
    ON emailregistration
    FOR EACH ROW
    BEGIN
    INSERT INTO answers (confirmed, unsubscribed)
    SELECT emailregistration.confirmed, emailregistration.unsubscribed
    FROM emailregistration
    WHERE emailregistration.email = email
    END;

I hope I described my problem accurately. Feel free to ask questions if I didnt. Thanks for your help, its greatly appreciated.

Maybe something like this

drop table if exists r,u;
create table r(email varchar(3), subscribed varchar(1), confirmed varchar(1));
create table u(email varchar(3), subscribed varchar(1), confirmed varchar(1));

insert into r (email) values('aaa'),('bbb');
insert into u (email,subscribed,confirmed) values('aaa','x','x'),('bbb','y','y');
drop trigger if exists t;
delimiter $$
CREATE TRIGGER t 
AFTER UPDATE
ON r
FOR EACH ROW
BEGIN
     update u 
        set  subscribed = new.subscribed,
              confirmed = new.confirmed
        where email = new.email;

END $$
delimiter ;

update r set subscribed = 'n' where email = 'aaa';

select * from r;

+-------+------------+-----------+
| email | subscribed | confirmed |
+-------+------------+-----------+
| aaa   | n          | NULL      |
| bbb   | NULL       | NULL      |
+-------+------------+-----------+
2 rows in set (0.00 sec)

select * from u;

+-------+------------+-----------+
| email | subscribed | confirmed |
+-------+------------+-----------+
| aaa   | n          | NULL      |
| bbb   | y          | y         |
+-------+------------+-----------+
2 rows in set (0.00 sec)

If this is not the model you have then add it to the question together with expected outcome.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM