Home >Database >Mysql Tutorial >How to Efficiently Delete Rows Across Tables Using JOIN?
Cross-Table Data Deletion Using JOIN
When managing multiple tables, deleting rows across tables based on specific criteria can be a common task. In this case, let's explore how to efficiently remove all rows from a table (Table1) whose IDs match those in another table (Table2).
To achieve this, we can utilize a JOIN clause in the DELETE statement. The following query will accomplish the task:
DELETE t1 FROM Table1 t1 JOIN Table2 t2 ON t1.ID = t2.ID;
In this query:
By executing this query, all rows in Table1 that have IDs present in Table2 will be deleted. This is an effective method for performing cross-table data deletion without the need for subqueries or complex IN clauses.
The above is the detailed content of How to Efficiently Delete Rows Across Tables Using JOIN?. For more information, please follow other related articles on the PHP Chinese website!