I have a users table and it is possible for one or more users to become related. I have a column in the database called referred and if it has an ID in it, if that ID gets deleted from the database, then the associated users must go too.
Here is my query:
START TRANSACTION;
DELETE FROM users
WHERE id IN
( SELECT a.id
FROM users a
JOIN users b ON a.referred = b.id );
If I remove user Josh Murray with id='1', anyone that matches criteria referred='1' must be deleted also.
cascadeoption will do the job