For more information about how to delete SQL statements when using MySQL multi-Table joined data, see.
For more information about how to delete SQL statements when using MySQL multi-Table joined data, see.
DELETE: How can I DELETE data from multiple associated tables at the same time? Here is an in-depth explanation:
The Code is as follows: |
|
1 delete from t1 where Condition 2 delete t1 from t1 where Condition 3 delete t1 from t1, t2 where Condition 4 delete t1, t2 from t1, t2 where conditions |
Category and news ).
The id (column number) Field in category is used as the primary key of the table. It uniquely identifies the information of a column.
The id field in news is used as the primary key of the table. It uniquely identifies the information of a column.
The category_id (column number) field is associated with the id field of the category table.
1. SQL deletion statement
The Code is as follows: |
|
Delete category, news from category left join news on category. id = news. category_id
|
1. Delete all matching records with id values in Table T2. 1
The Code is as follows: |
|
DELETE t1 FROM t1, t2 WHERE t1.id = t2.id or delete from t1 USING t1, t2 WHERE t1.id = t2.id |
2. Search for and delete no matching records in data table T2.
The Code is as follows: |
|
DELETE t1 FROM t1 left join T2 ON t1.id = t2.id WHERE t2.id is null or delete from t1, USING t1 left join T2 ON t1.id = t2.id WHERE t2.id IS NULL |
3. Find the data with the same record from the two tables and delete the data from both tables. 1
The Code is as follows: |
|
DELETE t1, t2 from t1 left join t2 ON t1.id = t2.id WHERE t1.id = 25 |
Note that delete t1, t1 in t2 from, and t2 cannot be aliases.
Example: 1
The Code is as follows: |
|
Delete t1, t2 from table_name as t1 left join table2_name as t2 on t1.id = t2.id where table_name.id = 25 |
Execution in the data is incorrect (MYSQL version is not less than 5.0 is acceptable in 5.0)
Write the preceding statement as 1.
The Code is as follows: |
|
Delete table_name, table2_name from table_name as t1 left join table2_name as t2 on t1.id = t2.id where table_name.id = 25
|