Delete: How can I delete data from multiple associated tables at the same time? Here is an in-depth explanation: 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 Condition
The first three are feasible, and 4th are not feasible. That is to say, a simple Delete statement cannot be used to delete data from multiple tables. However, you can create a cascading Delete relationship between two tables, you can delete the data in another table at the same time.
1. Delete all records whose ID values match in data table T2 from data table T1 1 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 1 Delete T1 from T1 left join T2 on t1.id = t2.id where t2.id is null or delete from T1 from Table T2, using T1 left join T2 on t1.id = t2.id where t2.id is null
3. Find the data of the same record from the two tables and delete the data in both tables. 1 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.
For example, 1 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)
The preceding statement is written as 1 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
Execution in the data is incorrect (MySQL version earlier than 5.0 is acceptable in 5.0)
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service