有一个表示地区的表,表结构与数据大概如下表。

ID NAME PARENT_ID
1 中国  
2 广东省 1
3 广州市 2
4 荔湾区 3
5 越秀区 3
6 番禺区 3
7 小谷围街道 6

现为了查询方便,需要加一列PARENT_NAME,用以表示上级地区的名称(虽然不符合第三范式,传递依赖,但有时为了业务上的可行性、便利性,可以按实际情况考虑)

ID NAME PARENT_ID PARENT_NAME
1 中国    
2 广东省 1  
3 广州市 2  
4 荔湾区 3  
5 越秀区 3  
6 番禺区 3  
7 小谷围街道 6  

附,表的DDL、DML:

-- ----------------------------
-- Table structure for `t_area`
-- ---------------------------- CREATE TABLE `t_area` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(256) DEFAULT NULL,
`parent_id` int(11) DEFAULT NULL,
`parent_name` varchar(256) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of t_area
-- ----------------------------
INSERT INTO `t_area` VALUES ('', '中国', null, null);
INSERT INTO `t_area` VALUES ('', '广东省', '', null);
INSERT INTO `t_area` VALUES ('', '广州市', '', null);
INSERT INTO `t_area` VALUES ('', '荔湾区', '', null);
INSERT INTO `t_area` VALUES ('', '越秀区', '', null);
INSERT INTO `t_area` VALUES ('', '番禺区', '', null);
INSERT INTO `t_area` VALUES ('', '小谷围街道', '', null);

这时,需要根据已有信息,更新PARENT_NAME的数据,就有了如下的SQL:

/* [Err] 1093 - You can't specify target table 't' for update in FROM clause */
update t_area t set t.parent_name = (select t2.name from t_area t2 where t.parent_id = t2.id);

报出“1093 - You can't specify target table 't' for update in FROM clause”的异常。意思,意思大约为,你不能指定更新的目标表在FROM子句中(英文不好,即使认识各个单词,串起来就不行了。。。)

就如文档所述“Currently, you cannot update a table and select from the same table in a subquery.”,见http://dev.mysql.com/doc/refman/5.5/en/update.html。

不知道MySQL为什么不允许这样操作,猜,可能是担心更新的表与查询的表为同一表会存在嵌套递归?还是担心效率的问题呢?

如果,将该表在嵌套一层,即“(select * from t_area) st”这样得出一个临时的结果集,即无报错,但,这性能较差,仅仅适合较小的数据量的。(见此讨论帖:http://stackoverflow.com/questions/17742214/you-cant-specify-target-table-name-for-update-in-from-clause)。

修改后如下:

--ok
update t_area t set t.parent_name = (select t2.name from (select * from t_area) t2 where t.parent_id = t2.id);

具体针对这个需求,更简单的方式,貌似也可以:

update t_area t, t_area t2 set t.parent_name = t2.name where t.parent_id = t2.id;

MySQL - 1093异常 - You can't specify target table 't' for update in FROM clause的更多相关文章

  1. ERROR 1093 (HY000): You can't specify target table 'test' for update in FROM clause

    MySQL执行更新语句报错: 更新语句:UPDATE test SET state=50 WHERE num IN(SELECT num FROM test WHERE state=60): 报错:E ...

  2. 【MySQL】解决You can't specify target table 'user_cut_record_0413' for update in FROM clause

    问题 You can't specify target table 'user_cut_record_0413' for update in FROM clause 原因 待更新/删除的数据集与查询的 ...

  3. mysql 报错You can't specify target table 'wms_cabinet_form' for update in FROM clause

    这个错误是说从t表select出来的无法又更新t表. 可以在select的时候先取个别名,弄个临时表即可.

  4. Mysql update in报错 [Err] 1093 - You can't specify target table 'company_info' for update in FROM clause

    Mysql update in报错 解决方案: [Err] 1093 - You can't specify target table 'company_info' for update in FRO ...

  5. MySQL中执行sql语句错误 Error Code: 1093. You can't specify target table 'car' for update in FROM clause

    MySQL中执行sql语句错误 Error Code: 1093. You can't specify target table 'car' for update in FROM clause 201 ...

  6. 关于mysql 5.7版本“报[Err] 1093 - You can't specify target table 'XXX' for update in FROM clause”错误的bug

    不同于oracle和sqlserver,mysql并不支持在更新某个表的数据时又查询了它,而查询的数据又做了更新的条件,因此我们需要使用如下的语句绕过: , notice_code ) a) ; 本地 ...

  7. MySQL 1093 - You can't specify target table 'sc' for update in FROM clause

    错误代码如下: #(8) 把"邓维杰"同学的成绩全部删除. SELECT * FROM sc WHERE EXISTS(SELECT * FROM student WHERE st ...

  8. mysql You can't specify target table 'sys_org_relation' for update in FROM clause 删除表条件不能直接包含该表

    mysql中You can't specify target table for update in FROM clause错误的意思是说,不能先select出同一表中的某些值,再update这个表( ...

  9. mysql中更新或者删除语句中子语句不能操作同一个表You can't specify target table 'test' for update in FROM clause

    问题描述:有个数据表test,有个字段value,如下 mysql> select * from test;+----+------------------------------------+ ...

随机推荐

  1. Log Sessions to Local Database

    Add Rules to Fiddler to create a new menu item as follows: // Log the currently selected sessions in ...

  2. TOMCAT清理

      CreateTime--2017年7月10日08:54:00Author:Marydon 如何清理TOMCAT 方式一:通过tomcat的安装目录进行清理 找到TOMCAT的根目录,如图: 实质: ...

  3. svn,git,scp,rsync,rake,ssh,wget,axel,aria2,nohup,grep,tail,siege,mitmproxy,ulimit,netstat,lsof

    scp 把本地文件上传到server上 scp -P 1234 config/cert/dev/client.pem dev@xx.xxx.xxx:/srv/rorapps/fgcc/config/c ...

  4. Java 爬虫(获取指定页面中所有的邮箱地址)

    import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import java.io.In ...

  5. JDBC实例--通过连接工具类DBUtil +配置文件来获取连接数据库,方便又快捷

    根据前面的连接方法,还有缺点就是,如果人家要换数据库,还得改动源代码,然后还要编译什么的.这样客户修改也不容易. 做法:我们写一个配置文件,把该数据写在配置文件上面,然后通过类来加载改文件,然后读取相 ...

  6. MySQL Desc指令相关

    MySQL Desc指令相关   2011-08-09 11:25:50|  分类: my基本命令 |举报 |字号 订阅 1.desc tablename; 例如 :mysql> desc jo ...

  7. 机器学习性能度量指标:AUC

    在IJCAI 于2015年举办的竞赛:Repeat Buyers Prediction Competition 中, 很多参赛队伍在最终的Slides展示中都表示使用了 AUC 作为评估指标:     ...

  8. Block编程注意的问题

    一,前言   block 是在 iOS 4 中引入的新特性,它和 C++ 11 中的 lamba 表达式概念相似,有时候也被称为闭包.经过一段时间的使用,我发现要用对用好 block 还是有不少需要注 ...

  9. js 排序

    在本例中,我们将创建一个数组,并按字母顺序进行排序: <script type="text/javascript"> var arr = new Array(6) ar ...

  10. AsyncTask与ProgressDialog使用笔记(安卓在背景运行耗时任务)

    AsyncTask用在需要在ui线程中调用.在背景线程中执行耗时任务.并且在ui线程中返回结果的场合.下面就是一个在背景中运行的AsyncTask的实现DownloadDBTask, Android中 ...