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.