在有些时候有级联关系的数据放在了同一张表中,在写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错误解决办法的更多相关文章

  1. 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这样的错误,它的意思是说,不能 ...

  2. [转]MySQL 中 You can't specify target table '表名' for update in FROM clause错误解决办法

    原文链接:https://blog.csdn.net/qq_29672495/article/details/72668008

  3. 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 这样的错误 错误含义 ...

  4. 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出同一表中的某些值 ...

  5. 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这个表( ...

  6. 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出同一表中的某些值 ...

  7. 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 ...

  8. 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 ...

  9. 【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 ...

随机推荐

  1. linux部署php网页

    liunx环境下运行php和传统的Java web程序不一样.我直接放在tomcat下竟然不行. 安装了php的一些环境,还是访问不了,原因是不支持https访问.没办法,还是得用tomcat,同时我 ...

  2. 单机prometheus vs 集群kube-prometheus+prometheus-operator

    prometheus 组件: node-exporter:9100端口 https://segmentfault.com/a/1190000017959127

  3. centos配置/etc/mail.rc发邮件

    安装mailx: yum install mailx vi /etc/mail.rc set from=524755798@qq.comset smtp="smtps://smtp.qq.c ...

  4. 《MySQL必知必会》学习笔记——前言

    前言 MySQL已经成为世界上最受欢迎的数据库管理系统之一.无论是用在小型开发项目上,还是用来构建那些声名显赫的网站,MySQL都证明了自己是个稳定.可靠.快速.可信的系统,足以胜任任何数据存储业务的 ...

  5. Qt5.编译错误.error: C2338: The slot requires more arguments than the signal provides.

    1.Qt563x86vs2015,遇到如下 编译错误: error: C2338: The slot requires more arguments than the signal provides. ...

  6. MApp_ZUI_CTL_MarqueeTextWinProc字串滚动

    /////////////////////////////////////////////////////////////////////////////// /// global MApp_ZUI_ ...

  7. 按键板的原理与实现----ADC

    在嵌入式系统产品开发中,按键板的设计是最基本的,也是项目评估阶段必须要考虑的问题.其实现方式又很多种,具体使用那一种就需要结合特定IC的可用IO数量,并综合考虑成本,做出最终选择.本系列文章将介绍多种 ...

  8. 常见的序列化框架及Protobuf序列化原理

    原文链接:https://www.jianshu.com/p/657fbf347934 https://www.cnblogs.com/javazhiyin/p/11375553.html https ...

  9. Use Hexo to Build My Gitee Blog

      之前有自己建站托管自己的博客系统, 后来因为流量实在太少, 服务器又要每个月出钱, 然后就把她关了, 然是拥有自己的网站的心一直没有退去啊, 然后之前有接触到别人用GitHub托管静态网页的玩法, ...

  10. MySQL(八)事务的隔离级别

    一.事务的并发问题 1.脏读:事务A读取了事务B更新的数据,然后B回滚操作,那么A读取到的数据是脏数据 2.不可重复读:事务 A 多次读取同一数据,事务 B 在事务A多次读取的过程中,对数据作了更新并 ...