MySQL中You can't specify target table '表名'('sn_app_label') for update in FROM clause错误解决办法
在有些时候有级联关系的数据放在了同一张表中,在写sql语句的时候可能会遇到这样的场景:我要插入两条数据,第一条是父节点,第二条是子节点,关联关系是父节点的自增长id;在写这样的sql语句时有可能就会出现You can't specify target table '表名' for update in FROM clause这种错误,意思就是:“不能先select出同一表中的某些值,再update这个表(在同一语句中)。”产生这个错误的sql如下:
INSERT INTO sn_app_label (app_code,apply_id,apply_name,level_id,parent_id,state,operation_time)
VALUES('CS001' ,'G001','门店属性标签','' ,'','','2018-06-15 00:00:00'); INSERT INTO sn_app_label (app_code, apply_id, apply_name, level_id, parent_id , state, operation_time)
VALUES ('CS001', '', '门店类型', '', (SELECT auto_id FROM sn_app_label WHERE app_code = 'CS001' AND apply_id = 'G001') , '', '2018-06-15 00:00:00');
sql错误位置为:

解决方法:既然不能在同一张表中,那么就使用中间表,即先select出所需要的字段auto_id,把auto_id放到一个中间表中,再从中间表中取出这auto_id;
正确sql如下
INSERT INTO sn_app_label (app_code,apply_id,apply_name,level_id,parent_id,state,operation_time)
VALUES('CS001' ,'G001','门店属性标签','' ,'','','2018-06-15 00:00:00'); INSERT INTO sn_app_label (app_code, apply_id, apply_name, level_id, parent_id , state, operation_time)
VALUES ('CS001', '', '门店类型', '', (SELECT a.auto_id FROM(SELECT auto_id FROM sn_app_label WHERE app_code = 'CS001' AND apply_id = 'G001')a), '', '2018-06-15 00:00:00');
错误位置修改为:

注:以上只针对MySQL
MySQL中You can't specify target table '表名'('sn_app_label') for update in FROM clause错误解决办法的更多相关文章
- MySQL 中 You can't specify target table '表名' for update in FROM clause错误解决办法
在MySQL中,写SQL语句的时候 ,可能会遇到You can't specify target table '表名' for update in FROM clause这样的错误,它的意思是说,不能 ...
- [转]MySQL 中 You can't specify target table '表名' for update in FROM clause错误解决办法
原文链接:https://blog.csdn.net/qq_29672495/article/details/72668008
- Mysql - You can't specify target table '表名' for update in FROM clause 错误解决办法
背景 在MySQL中,写SQL语句的时候 ,可能会遇到 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 <tbl> for update in FROM clause错误的意思是说,不能先select出同一表中的某些值 ...
- mysql中You can’t specify target table 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 for update in FROM clause异常
mysql中You can't specify target table <tbl> for update in FROM clause错误的意思是说,不能先select出同一表中的某些值 ...
- Mysql中You can't specify target table for update in FROM clause错误的意思是说,不能先select出同一表中的某些值,再update这个表(在同一语句中)。
将select出的结果再通过中间表select一遍,这样就规避了错误.注意,这个问题只出现于mysql,mssql和oracle不会出现此问题. mysql中You can't specify tar ...
- MySQL--mysql中You can’t specify target table for update in FROM clause错误解决方法
参考:http://www.jb51.net/article/60926.htm mysql中You can't specify target table for update in FROM cla ...
- 【mysql 】sql删除重复记录 You can't specify target table '表名' for update in FROM clause
用下面的语句就报语法出错: delete from tab_record where recordid not in (select min(c.recordid) as recordid from ...
随机推荐
- postgre alter命令修改字段
参考文档:https://www.yiibai.com/postgresql/postgresql_alter_command.html PostgreSQL ALTER TABLE命令用于添加,删除 ...
- 【转载】 机器学习实战 - 读书笔记(07) - 利用AdaBoost元算法提高分类性能
原文地址: https://www.cnblogs.com/steven-yang/p/5686473.html ------------------------------------------- ...
- asp中出现“错误 '80040e14' FROM 子句语法错误”原因
当你的sql语句中出现 “错误 '80040e14' FROM 子句语法错误.”错误时,请注意了,那有可能是你的表名的命名不规范造成的,比如你的表名是“user”那么这杨的表名是不行的,那么在sql语 ...
- REDIS类和方法说明
package zhengxin.core; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; imp ...
- jquery创建一个新的节点对象(自定义结构/内容)的好方法
jq创建一个新的节点对象,这对一些自定义功能很有帮助,而且可以随意控制对象的结构与内容,何乐而不为呢,看到这里,相信有些朋友已经按耐不住了,好记下来为大家介绍实现方法,感兴趣的朋友可以了解下哦 < ...
- 【Leetcode_easy】1137. N-th Tribonacci Number
problem 1137. N-th Tribonacci Number solution: class Solution { public: int tribonacci(int n) { ) ; ...
- 内层元素设置position:relative后父元素overflow:hidden overflow:scroll失效 解决方法
内层元素设置position:relative后父元素overflow:hidden overflow:scroll 都失效 解决方法:在position:relative的外层父容器加positio ...
- 09点睛Spring MVC4.1-异步请求处理(包含兼容浏览器的服务器端推送)
转发地址:https://www.iteye.com/blog/wiselyman-2215852 9.1 异步请求处理 Servlet 3开始支持异步请求处理 Spring MVC 3.2开始支持S ...
- 13点睛Spring4.1-Spring EL
13.1 Spring EL Spring EL-Spring表达式语言,支持在xml和注解中使用表达式,类似jsp的EL表达式语言; 本教程关注于在注解中使用Spring EL; Spring EL ...
- web端自动化——Remote应用
Selenium Grid允许同时并行地.在不同的环境上运行多个测试任务.这里主要演示一下怎么使用Selenium Grid. 准备: 1. 需要两台机子 2.两台机子分别安装好JDK环境 3.两台机 ...