MySQL Multiple table association data Delete SQL statements at the same time

Source: Internet
Author: User

Delete Deletes multiple table data, how can you delete data from multiple relational tables at the same time? Here's an in-depth explanation:

The code is as follows Copy Code
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

Category (Column information table) and news (press data sheet).

The ID (column number) field in the category is used as the primary key (primary key) for the table. The only information that identifies a column.
The ID field in news is the primary key (primary key) for the table. The only information that identifies a column.

The category_id (column number) field is associated with the ID field of the category table.

1.SQL DELETE statement

The code is as follows Copy Code


Delete category,news from category left JOIN news on category.id = news.category_id


1, from the data table t1 those ID values in the datasheet T2 have a matching record of all deleted 1

The code is as follows Copy Code

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 1

The code is as follows Copy Code

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 1

The code is as follows Copy Code
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: 1

The code is as follows Copy Code

Delete T1,T2 from table_name as T1 left join Table2_name as T2 on T1.id=t2.id where table_name.id=25

Executing in the data is wrong (MYSQL version is not less than 5.0 in 5.0 is OK)

The above statement is rewritten as 1

  code is as follows copy code
 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

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.