MySQL allows you to delete rows from one or more tables using a JOIN. This is useful when the rows to delete are identified based on data in a related table, or when you need to delete matching rows from multiple tables simultaneously.
Single-Table DELETE with JOIN Filter
You can join another table to filter which rows to delete from the primary table. Only the primary table (listed after DELETE) has its rows removed; the joined table is used only for the filter condition.
Multi-Table DELETE
By listing multiple table aliases after DELETE, you can remove rows from several tables in a single statement. This is useful for maintaining referential integrity when foreign key constraints are not in place.
- List the aliases of tables to delete from after the DELETE keyword
- Define the FROM and JOIN clauses as usual
- Only rows in the listed aliases are deleted
- The operation is atomic within a transaction