MySQL - 1093异常 - You can't specify target table 't' for update in FROM clause
有一个表示地区的表,表结构与数据大概如下表。
| 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的更多相关文章
- 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 ...
- 【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 原因 待更新/删除的数据集与查询的 ...
- mysql 报错You can't specify target table 'wms_cabinet_form' for update in FROM clause
这个错误是说从t表select出来的无法又更新t表. 可以在select的时候先取个别名,弄个临时表即可.
- 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 ...
- 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 ...
- 关于mysql 5.7版本“报[Err] 1093 - You can't specify target table 'XXX' for update in FROM clause”错误的bug
不同于oracle和sqlserver,mysql并不支持在更新某个表的数据时又查询了它,而查询的数据又做了更新的条件,因此我们需要使用如下的语句绕过: , notice_code ) a) ; 本地 ...
- 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 ...
- 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这个表( ...
- mysql中更新或者删除语句中子语句不能操作同一个表You can't specify target table 'test' for update in FROM clause
问题描述:有个数据表test,有个字段value,如下 mysql> select * from test;+----+------------------------------------+ ...
随机推荐
- HDU 4847 陕西邀请赛A(水)
HDU 4847 Wow! Such Doge! pid=4847" style="">题目链接 题意:给定文本,求有几个doge,不区分大写和小写 思路:水题.直 ...
- java 读写csv
import java.io.IOException; import java.nio.charset.Charset; import com.csvreader.CsvReader; import ...
- 编译时:virtual memory exhausted: Cannot allocate memory(转)
一.问题 当安装虚拟机时系统时没有设置swap大小或设置内存太小,编译程序会出现virtual memory exhausted: Cannot allocate memory的问题,可以用swap扩 ...
- 网站Web性能测试:ApacheBench,Webbench,http_load使用教程
网站Web性能测试:ApacheBench,Webbench,http_load使用教程 Web服务器 欲思 10个月前 (05-25) 0评论 一个网站或者博客到底能够承受多大的用户访问量经常是 ...
- 用户研究Q&A(1)
近来,不少同事开始认同用户研究的价值,希望通过接触,理解和研究用户来获取提升产品的有效信息.这绝对是件好事,因为我一直抱持的理念是,研究并不是藏在实验室或者握在少部分人手中的稀罕货,更重要是一种理念和 ...
- Toast连续弹出的问题
public class CommUtils { private static Toast toast = null; public static void showToast(int text) { ...
- PHP中的密码加密的解决方案
层出不穷的类似事件对用户会造成巨大的影响,因为人们往往习惯在不同网站使用相同的密码,一家“暴库”,全部遭殃 一般的解决方案 1.将明文密码做单向hash $password = md5($_POST[ ...
- C实现9种排序算法
算法复杂度以及稳定性分析 算法名称 平均时间 辅助空间 稳定性 冒泡排序 O(n2) O(1) 是 选择排序 O(n2) O(1) 否 插入排序 O(n2) O(1) 是 自底向上归并排序 O(nlo ...
- python接口自动化(二十七)--html 测试报告——上(详解)
简介 上一篇我们批量执行完用例后,生成的测试报告是文本形式的,不够直观,而且报告一般都是发给leader的,所以最好是直观一目了然,为了更好的展示测试报告,最好是生成 HTML 格式的.unittes ...
- Android之SurfaceView使用总结
1.概念SurfaceView是View类的子类,可以直接从内存或者DMA等硬件接口取得图像数据,是个非常重要的绘图视图.它的特性是:可以在主线程之外的线程中向屏幕绘图上.这样可以避免画图任务繁重的时 ...