MySQL: [ERR] 1093-you can ' t specify target table ' BK ' for update on FROM clause

Source: Internet
Author: User

Incorrectly, you cannot select some values from the same table first, and then update the table (in the same statement). For example, the following SQL:

Delete from the TBL where ID in
(
Select Max (ID) from TBL a where EXISTS
(
Select 1 from the TBL B where A.tac=b.tac GROUP by TAC has count (1) >1
)
Group BY TAC
)

Rewrite it to the following line:

Delete from the TBL where ID in
(
Select a.ID from
(
Select MAX (ID) ID from tbl a where EXISTS
(
Select 1 from the TBL B where A.tac=b.tac GROUP by TAC has count (1) >1
)
Group BY TAC
) A
)

Again as: UPDATE BK_YGSBK as bk SET report_visit = (SELECT COUNT (*) from BK_YGSBK where Zyid = ' 1 ') where BK. Zyid = ' 1 '

Should be changed to: UPDATE BK_YGSBK as bk SET report_visit = (SELECT * FROM [select COUNT (*) from BK_YGSBK WHERE Zyid = ' 1 ') as R) wher E BK. Zyid = ' 1 '

In other words, the result of select is passed through the intermediate table select again, thus avoiding the error. Note that this issue only occurs in Mysql,mssql and Oracle does not occur.

MySQL: [ERR] 1093-you can ' t specify target table ' BK ' for update on FROM clause

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.