MySQL Multiple Table association update/DELETE SQL statement

Source: Internet
Author: User

1, MySQL multiple table association Delete use alias, TBLWENHQ is the real table name, A is TBLWENHQ alias, B is another table name

The code is as follows Copy Code

DELETE A from TBLWENHQ a,b where a.id=b.id

2. When using MySQL for the delete from operation, an error occurs if the FROM clause of the subquery and the update/delete object use the same table.

DELETE from tab1 WHERE col1 = (SELECT MAX (col1) from TAB1);

ERROR 1093 (HY000): You can ' t specify target table ' tab1′for update in FROM clause

Correct usage: DELETE from tab1 WHERE col1 = (SELECT MAX (col1) from Tab1 as a);

Table Associated Update Notes

The code is as follows Copy Code

UPDATE b,a SET b.public=a.public WHERE b.id=a.id

Cases

Perform updates on a single table nothing more to say than to update table_name set col1 = Xx,col2 = yy where col = zz, which is mainly the setting of the Where condition. Sometimes updating a table may involve more than one datasheet, for example:

The code is as follows Copy Code

Update Table_1 Set score = score + 5 where UID in (select UID from table_2 where sid = 10);

In fact, update can also be used to the left join, inner join to the association, may be more efficient execution, the above SQL to replace the
Join in the following ways:

The code is as follows Copy Code

Update table_1 t1 INNER join table_2 t2 on t1.uid = T2.uid Set score = score + 5 where T2.sid = 10;


Example 1

MySQL Multiple table association data deletion at the same time

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

  code is as follows copy code

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

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.