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 ...
随机推荐
- ubuntu上安装jdk
使用安装包安装:JDK官网下载地址: https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.h ...
- ionic4.x网络请求
创建common服务: import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/ ...
- 十、collection的作用+变量
一.collection作用?容器 组织业务逻辑 导入导出 其他功能,比如监控和mock server 二.为什么要使用变量 假设我们需要测试n个api,这些api的domain都是相同的,比如 ap ...
- Linux expect的安装与使用
Expect是在Tcl的基础上创建的,可以用来做一些Linux下无法做到交互的命令操作,可用于远程管理服务器. 一.安装Tcl: 1.下载源码包: wget http://nchc.dl.source ...
- NSGA,NSGA-II,Epsilon-MOEA,DE C语言Deb教授原版代码
NSGA,NSGA-II,Epsilon-MOEA,Basic Differential Evolution (DE) C语言Deb教授原版代码地址 觉得有用的话,欢迎一起讨论相互学习~[Follow ...
- BladeX部署说明(win7)
一:安装jdk-8u221-windows-x64 二:安装Nacos,解压nacos-server-1.0.0,运行bin目录下的startup.cmd,成功可以用浏览器访问:localhost:8 ...
- 解决HTM或HTML的图标
HTM和HTML的文件图标不能正常显示,显示为无关联应用程序的白板图标,搞了很久都没能解决,最后综合了几种方法才“搞定”她!出现这种情况的原因可能是安装了某些软件(比如OFFICE.FIREFOX)后 ...
- Kafka运维大全来了!优化、监控、故障处理
Kafka运维大全来了!优化.监控.故障处理…… Kafka概念 Kafka是分布式发布-订阅消息系统.它最初由LinkedIn公司开发,之后成为Apache项目的一部分.Kafka是一个分布式的 ...
- npm i node-sass 报错&npm 镜像切换
npm install --save node-sass --registry=https://registry.npm.taobao.org --disturl=https://npm.taobao ...
- beyond Compare 30天过期后的处理办法
打开Beyond Compare 4,提示已经超出30天试用期限制,解决方法: 修改C:\Program Files\Beyond Compare 4\BCUnrar.dll,这个文件重命名或者直接删 ...