Mysql has a special problem: You can "t specify target table" wms_cabinet_form"

Source: Internet
Author: User

I encountered a special problem writing mysql today.
The mysql statement is as follows:

Update wms_cabinet_form set cabf_enabled = 0
Where cabf_id in (
SELECT wms_cabinet_form.cabf_id FROM wms_cabinet_form
Inner Join wms_cabinet ON wms_cabinet_form.cabf_cab_id = wms_cabinet.cab_id
Inner Join wms_cabinet_row ON wms_cabinet.cab_row_id = wms_cabinet_row.row_id
Where wms_cabinet_row.row_site_id = 27 and wms_cabinet_form.cabf_enabled = 1)

The following prompt is prompted during running: You can't specify target table 'wms _ cabinet_form 'for update in FROM clause

Run the select statement in the in statement:

SELECT wms_cabinet_form.cabf_id FROM wms_cabinet_form
Inner Join wms_cabinet ON wms_cabinet_form.cabf_cab_id = wms_cabinet.cab_id
Inner Join wms_cabinet_row ON wms_cabinet.cab_row_id = wms_cabinet_row.row_id
Where wms_cabinet_row.row_site_id = 27 and wms_cabinet_form.cabf_enabled = 1

You can select the correct result correctly. Then write the result directly into in. The modified statement is as follows:
Update wms_cabinet_form set cabf_enabled = 0 where cabf_id in ('000000', '000000', '000000'), and then run the command to correctly execute the update.
At the beginning of this step, I cannot understand why an error occurs when running the select clause? In the past, mssql was a common method.
No way. Only baidu can be used. Find two records.

The original reason is: mysql cannot be used in this way. (Wait for mysql to upgrade ). The English error message indicates that some values in the same table cannot be selected first,
Update the table (in the same statement ). We also found an alternative solution and rewritten the SQL statement.

The rewritten SQL statement is as follows.

Update wms_cabinet_form set cabf_enabled = 0 where cabf_id in (
SELECT a. cabf_id FROM (select tmp. * from wms_cabinet_form tmp)
Inner Join wms_cabinet B ON a. cabf_cab_id = B. cab_id
Inner Join wms_cabinet_row c ON B. cab_row_id = c. row_id
Where c. row_site_id = 29 and a. cabf_enabled = 1)

Focus on SELECT a. cabf_id FROM (select tmp. * from wms_cabinet_form tmp) a, I select tmp. * from wms_cabinet_form tmp as a subset,
Then select a. cabf_id FROM subset, so that the select and update Tables are not the same. This problem is solved perfectly.

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.