A more specific problem with MySQL: You can ' t specify target table ' Wms_cabinet_form ' _mysql

Source: Internet
Author: User
Today in writing MySQL encountered a more specific problem.
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 runtime presents the following prompts: You can ' t specify target table ' Wms_cabinet_form ' for update in FROM clause

To run the select words in Inside:

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

Correct results can be select correctly. The result is written directly into in, and the following sentence is changed:
Update wms_cabinet_form set cabf_enabled=0 where cabf_id in (' 113 ', ' 114 ', ' 115 '), and then run to perform the update correctly.
At this point, I don't understand why running with the SELECT clause would make a mistake. Previously in MSSQL this kind of writing is very common.
No way, only the use of Baidu. Two records found.

The original reason is: MySQL can not be used in this way. (Wait for MySQL upgrade). That string of English errors means that you cannot select certain values in the same table first.
Update this table again (in the same statement). A workaround is also found, overwriting the SQL.

The rewritten SQL is shown below, and you will be careful to distinguish between them.

Update wms_cabinet_form set cabf_enabled=0 where cabf_id in (
Select a.cabf_id from (select tmp.* from Wms_cabinet_form tmp) a
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)

Focusing 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 the a.cabf_id from subset so that neither the Select nor the update are the same table. 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.