D
D
Den182022-04-10 17:06:07
MySQL
Den18, 2022-04-10 17:06:07

How to delete from three or more mysql tables by one id?

For removal I use request from the documentation.

DELETE FROM userdata, moneys, user_roles USING userdata INNER JOIN moneys INNER JOIN user_roles WHERE userdata.userid=525300849082105856 AND moneys.userid=525300849082105856 AND user_roles.userid=525300849082105856;

But a difficulty arises when one of the tables has no records with this userid, and the query does not delete any records in other tables.
Tell me how to change the request.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Akina, 2022-04-10
@Den18

DELETE 
FROM userdata, moneys, user_roles 
USING ( SELECT 525300849082105856 userid ) criteria
LEFT JOIN userdata USING (userid)
LEFT JOIN moneys USING (userid)
LEFT JOIN user_roles USING (userid);

R
rPman, 2022-04-10
@rPman

and left join?
According to the right way, when creating a foreign key index, you need to specify on delete cascade, and then when you delete a record in the main table, all related records will be deleted automatically

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question