Delete another table based on the table

Source: Internet
Author: User


How can I delete data from multiple associated tables at the same time when I DELETE multiple table data based on the DELETE operation of another table? Here I have made an in-depth explanation: www.2cto.com 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 are feasible for the first three, 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 with IDs in Table T2. DELETE t1 FROM t1, t2 WHERE t1.id = t2.id or delete from t1 USING t1, t2 WHERE t1.id = t2.id 2. FROM data table t1, no matching records in data table t2 are found and deleted. 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 of the same record in two tables and DELETE t1 from both tables, t2 from t1 left join t2 ON t1.id = t2.id WHERE t1.id = 25 note that delete t1 here, t1 in t2 from, t2 cannot be an alias such as: delete t1, t2 from table_name as t1 left join table2_name as t2 on t1.id = t2.id where table_name.id = 25 the execution in the data is incorrect (MYSQL version no less than 5.0 is acceptable in MYSQL 5.0) the preceding statement is written as 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 versions earlier than 5.0 are acceptable in MYSQL 5.0)
 

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.