简体   繁体   中英

MySQL delete rows from other tables if deleted from main table

I have added a custom table in Wordpress DB called wp_likeandfollow, it contains these 4 columns

ID====following_id====follower_id====network_id

Here follower_id and following_id are the actual ID of users and there can be duplicate entries. I want to have a check that if admin deletes a user from admin panel, then all entries of that user in likeandfollow table should also be deleted automatically and I want this to be done through MySQL automatically (not via PHP).

Thank you in advance.

You can use a trigger for this.

CREATE TRIGGER remove_likes_and_follows AFTER DELETE on wp_users
FOR EACH ROW
    BEGIN
    DELETE FROM wp_likeandfollow
        WHERE wp_likeandfollow.following_id = old.id;
END

where old.id will be wp_users.id in this case.

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