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

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. 算法笔记_176:历届试题 最大子阵(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 给定一个n*m的矩阵A,求A中的一个非空子矩阵,使这个子矩阵中的元素和最大. 其中,A的子矩阵指在A中行和列均连续的一块. 输入格式 输入 ...

  2. windows命令行设置IP与DNS

    用dos命令修改IP等本地连接属性 平时我们改IP通常都在是窗口界面本地连接直接修改, 那在命令行也可以设置IP地址?当然可以,这里要用到netsh命令 .点击“开始”->“运行”,输入“cmd ...

  3. Eclipse 如何创建Web项目

      Eclipse 如何创建Web项目 CreateTime--2018年3月8日16:43:33 Author:Marydon 第一步: 右键-->New-->Dynamic Web P ...

  4. 为什么WEB-INF外的jsp无法根据cookie享受国际化

    243行走WEB-INF外则获取为空,走springmvc则可以获取到:

  5. java IO流之文件切割两例(含Properties 用法)

    package cn.itcast.io.p1.splitfile; import java.io.File;import java.io.FileInputStream;import java.io ...

  6. C# 的Timer 在javascript中的实现--基于Typescript

    class Timer { //js 内置的timer对象 private _jsInnerTimerObj: any; private _enable: boolean; private _hand ...

  7. history设置时间戳

    Linux查看历史命令,很关键!history,默认没有时间戳... 01.设置系统环境变量 echo 'export HISTTIMEFORMAT="%F %T  `whoami` &qu ...

  8. highstock高级篇之股票分时图

    一直在用 highchart 在做图表,最近一段时间突然接到一活,需要用 highstock 帮客户完成一个股票K线图和分时图.虽然用法和 api上与 highchart 没什么区别,但还是研究一番做 ...

  9. 【redis】常用命令

    三.常用命令    1)连接操作命令    quit:关闭连接(connection)    auth:简单密码认证    help cmd: 查看cmd帮助,例如:help quit         ...

  10. 很轻很强大:轻量级桌面环境比较(转自linuxeden)

    这天你终于下定决心购买了一台流行的 Netbook ,与往常装机一样,直接安装心爱的 Linux 发行版.好不容易安装完成了,却发现平日启动飞快的应用程序在 Netbook 上怎么都跑不快.怎么办呢? ...