mysql a more special problem: You can't specify target table 'wms_cabinet_form' for update in F

Today, I encountered a special problem in writing mysql.
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 prompts the following: You can't specify target table 'wms_cabinet_form' for update in FROM clause

Run the select clause in 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 correct result can be selected correctly. Then write the result directly into in, and the modified statement is as follows: 

update wms_cabinet_form set cabf_enabled=0 where cabf_id in ('113','114','115'), and then run it to perform the update correctly.

At this point, I can't understand why it is wrong to use the select clause to run? In the past, this way of writing in mssql was very common.
No way, only use baidu. Two records were found.

The original reason is: mysql can not be used in this way. (Wait for the mysql upgrade). The string of English error prompts means that some values ​​in the same table cannot be selected first.

Then update the table (in the same statement). Also found an alternative, rewrote the sql.

 

The rewritten sql is shown below, please distinguish carefully.

 

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 a.cabf_id FROM subset, so that both select and update will not be the same table. The problem is solved perfectly.



Reference article:
http://zhidao.baidu.com/question/68619324.html
http://blog.163.com/xiaoqiu_1120/blog/static/121632322007112411424982/

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326981177&siteId=291194637