mysql 一个较特殊的问题:You can't specify target table 'wms_cabinet_form' for update in FROM clause
2010-07-07 17:33
686 查看
原文地址:http://www.cnblogs.com/chy1000/archive/2010/03/02/1676282.html
今天在写 mysql 遇到一个比较特殊的问题。
mysql 语句如下:
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)
运
行时提出如下提示: You can't specify target table 'wms_cabinet_form' for update
in FROM clause
运行 in 里面的 select 字句:
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
可以正确 select 正确结果。再把结果直接写到
in 里面,改后语句如下:
update wms_cabinet_form set cabf_enabled=0 where
cabf_id in ('113','114','115'),再运行可以正确执行更新。
到这一步开始想不明白,为什么用
select 子句运行会出错呢?以前在 mssql 这种写法是很常见的。
没办法了,唯有动用 baidu。找到两条记录。
原
来原因是:mysql中不能这么用。 (等待mysql升级吧)。那串英文错误提示就是说,不能先select出同一表中的某些值,
再
update这个表(在同一语句中)。 也找到替代方案,重写改写了 sql 。
改写后的 sql
如下所示,大家仔细区别一下。
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)
重
点在 SELECT a.cabf_id FROM (select tmp.* from wms_cabinet_form tmp) a ,我
select tmp.* from wms_cabinet_form tmp 作为子集,
然后再 select a.cabf_id
FROM 子集,这样就不会 select 和 update 都是同一个表。致此问题得到完美解决。
参考文章:
http://zhidao.baidu.com/question/68619324.html http://blog.163.com/xiaoqiu_1120/blog/static/121632322007112411424982/
今天在写 mysql 遇到一个比较特殊的问题。
mysql 语句如下:
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)
运
行时提出如下提示: You can't specify target table 'wms_cabinet_form' for update
in FROM clause
运行 in 里面的 select 字句:
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
可以正确 select 正确结果。再把结果直接写到
in 里面,改后语句如下:
update wms_cabinet_form set cabf_enabled=0 where
cabf_id in ('113','114','115'),再运行可以正确执行更新。
到这一步开始想不明白,为什么用
select 子句运行会出错呢?以前在 mssql 这种写法是很常见的。
没办法了,唯有动用 baidu。找到两条记录。
原
来原因是:mysql中不能这么用。 (等待mysql升级吧)。那串英文错误提示就是说,不能先select出同一表中的某些值,
再
update这个表(在同一语句中)。 也找到替代方案,重写改写了 sql 。
改写后的 sql
如下所示,大家仔细区别一下。
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)
重
点在 SELECT a.cabf_id FROM (select tmp.* from wms_cabinet_form tmp) a ,我
select tmp.* from wms_cabinet_form tmp 作为子集,
然后再 select a.cabf_id
FROM 子集,这样就不会 select 和 update 都是同一个表。致此问题得到完美解决。
参考文章:
http://zhidao.baidu.com/question/68619324.html http://blog.163.com/xiaoqiu_1120/blog/static/121632322007112411424982/
相关文章推荐
- mysql 一个较特殊的问题:You can't specify target table 'wms_cabinet_form' for update in FROM clause
- mysql 一个较特殊的问题:You can't specify target table 'wms_cabinet_form' for update in FROM clause
- mysql 一个较特殊的问题:You can't specify target table 'wms_cabinet_form' for update in FROM clause
- (原)mysql错误1093 You can't specify target table 'wms_cabinet_form' for update in FROM clause
- mysql 一个较特殊的问题:You can't specify target table '表名' for update in FROM clause
- You can't specify target table 'wms_cabinet_form' for update in FROM clause
- mysql中的一个特殊问题:You can’t specify target table ‘abc’ for update in FROM clause
- MySQL "You can't specify target table 'X' for update in FROM clause" 错误解决方法
- MySQL报错“ you can't specify the target table for ‘update’ in from clause”
- mysql 更新sql报错:You can't specify target table 'wms_cabinet_form' for update in FROM clause
- 【mysql 】sql删除重复记录 You can't specify target table '表名' for update in FROM clause
- MYSQL 临时表 #1093 - You can't specify target table 'dealer_admin' for update in FROM clause
- 解决mysql不能修改(You can't specify target table 'basic_data' for update in FROM clause)
- 移动文章分类的sql语句,查询并更新同一个表的方法 You can't specify target table 'news_articles' for update in FROM clause
- MYSQL 1093 - You can't specify target table '' for update in FROM clause
- [Django](1093, "You can't specify target table 'fee_details_invoices' for update in FROM clause") 错误
- ERROR 1093 (HY000): You can't specify target table 'b' for update in FROM clause
- You can't specify target table 'table_name' for update in FROM clause
- MySQL问题:You can't specify target table '表名' for update in FROM clause
- MYSQL问题:You can't specify target table '表名' for update in FROM clause