mysql常见问题--target table for update in FROM clause
2016-09-09 12:47
477 查看
最近接触到一个项目,需要对地区表进行操作,其需求就是:将市级地区和县级地区的地区名中的“市”“县”去除。比如“上海市”–>“上海”。在数据表中地区用一个字段表示dis_name.
首先想到的是用sql中replace函数:
但这样的方式显得有点太粗糙了,如果在一些特殊的市级地区中也还有“市”字,如”历市镇”这样的镇级地区名会产生错误,所以没有用这种方式。
于是想用字符串截取的方式进行更新,主要是对市级地区最后一个“市”字进行删除即可。于是写出以下的sql语句
问题出现:You can’t specify target table ‘district’ for update in FROM clause
从字面上看:MySQL不能指定更新的目标表在FROM子句。即:不能先select出同一表中的某些值,再update这个表(在同一语句中)
解决方案:将select出的结果再通过中间表select一遍,这样就规避了错误。即:先把要更新的目标放到一张临时表再把要删除的条件指定到这张临时表即可。
由上,写出如下
问题解决!
首先想到的是用sql中replace函数:
update district set dis_name=replace(dis_name,'市',' ');
但这样的方式显得有点太粗糙了,如果在一些特殊的市级地区中也还有“市”字,如”历市镇”这样的镇级地区名会产生错误,所以没有用这种方式。
于是想用字符串截取的方式进行更新,主要是对市级地区最后一个“市”字进行删除即可。于是写出以下的sql语句
update district set dis_name = SUBSTR(dis_name,1,LENGTH(dis_name)/3-1) where district.dis_id in (select dis_id from district where dis_level!=1 and SUBSTR(dis_name,-1,1)='市')
问题出现:You can’t specify target table ‘district’ for update in FROM clause
从字面上看:MySQL不能指定更新的目标表在FROM子句。即:不能先select出同一表中的某些值,再update这个表(在同一语句中)
解决方案:将select出的结果再通过中间表select一遍,这样就规避了错误。即:先把要更新的目标放到一张临时表再把要删除的条件指定到这张临时表即可。
由上,写出如下
update district set dis_name = SUBSTR(dis_name,1,LENGTH(dis_name)/3-1) where district.dis_id in (select a.dis_id from (select dis_id from district where dis_level!=1 and SUBSTR(dis_name,-1,1)='市') as a)
问题解决!
相关文章推荐
- mysql update语句问题:You can't specify target table 'user' for update in FROM clause
- MYSQL问题:You can't specify target table '表名' 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 'wms_cabinet_form' for update in FROM clause
- MySQL can’t specify target table for update in FROM clause
- mysql中You can’t specify target table for update in FROM clause错误解决方法
- you can't specify target table for update in from clause Mysql 报错
- mysql中You can’t specify target table 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错误
- Mysql 错误 Code: 1093. You can't specify target table 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错误
- MySQL can’t specify target table 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错误
- mysql 出现You can't specify target table for update in FROM clause错误的解决方法
- MySQL报错“ you can't specify the target table for ‘update’ in from clause”
- MySQL:You can’t specify target table ‘A’ for update in FROM clause
- MySQL can’t specify target table for update in FROM clause