Delete Table 1 condition is data in another table, multiple table join Delete (GO)

Source: Internet
Author: User
Tags mysql version one table

Delete Deletes multiple table data, how can you delete data from multiple relational tables at the same time? Here's 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 3 are feasible, and the 4th is not.

That is, simple with the DELETE statement cannot do multiple table delete data operations, but you can establish a cascade delete, the two tables to establish a cascade delete relationship, you can implement the data deleted one table, while deleting the related data in another table.

1, from the data table t1 those ID values in the datasheet t2 have matching records are deleted

Delete T1 from t1,t2 where t1.id=t2.id or DELETE from T1 USING t1,t2 where t1.id=t2.id

2, from the datasheet T1 in the data table T2 no matching records to find out and delete

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 WH ERE T2.id is NULL

3, from the two tables to find the same record of data and two of the data in the table deleted

DELETE t1,t2 from T1 left JOIN T2 on T1.id=t2.id WHERE t1.id=25

Note that the t1,t2 in the delete t1,t2 from here 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 execution in the data is incorrect (MYSQL edition This is not less than 5.0 in 5.0 is OK)

The above statement is rewritten 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 in the data Face execution is wrong (MYSQL version is less than 5.0 in 5.0 is OK)

Transfer from (http://llsljh.iteye.com/blog/708064)

Related Article

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.