在有些时候有级联关系的数据放在了同一张表中,在写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. MySQL there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause同时创建多个更新当前时间戳字段 解决方法

    问题重现 在写这篇文章之前,明确我的MySQL版本,MariaDB 或者你使用 MySQL 8 也会出现如下问题 MySQL 版本 现在有这样的需求,一张表中有一个字段created_at记录创建该条 ...

  2. 谷歌官方 构建您的第一个应用 Kotlin版

    https://developer.android.google.cn/training/basics/firstapp MainActivity.kt package com.example.myf ...

  3. 解决 service iptables save 报错 please try to use systemctl

    本文档根据 service iptables save 报错 please try to use systemctl 提供解决方案.报错 [root@Jaking ~]# service iptabl ...

  4. 获取Excel中的图片

    如下图,上传要获取这里面的图片,而又不能直接选择,怎么办呢? 1.首先复制一份Excel文件命名copy.xlsx 2.修改copy.xlsx文件的后缀名变成copy.rar 3.解压copy.rar ...

  5. 【Java】Spring快速入门(一)

    Spring介绍 Spring可以轻松创建Java企业应用程序.它提供了在企业环境中使用Java语言所需的一切,支持Groovy和Kotlin作为JVM上的替代语言,并可根据应用程序的需要灵活地创建多 ...

  6. 使用 Ninja 代替 make

    使用 Ninja 代替 make 摘自:https://www.jianshu.com/p/d118615c1943 22017.01.14 11:41:44字数 1408阅读 26336 前言 在传 ...

  7. ArrayList数组操作

    String字符类型的操作方法 public static void main(String[] args) { // ArrayList ArrayList<String> list = ...

  8. 转:HR schema

    ###createe RemRem $Header: hr_cre.sql 29-aug-2002.11:44:03 hyeh Exp $RemRem hr_cre.sqlRemRem Copyrig ...

  9. spring 传播行为与数据库事务ACID

    数据库事务ACID特性 数据库事务正确执行的4个基础要素是原子性(Atomicity).一致性(Consistency).隔离性(Isolation)和持久性(Durability). •原子性:整个 ...

  10. sharp.js中文文档

    高性能node.js图像处理库,使用libvips库来实现. 英文地址:sharp.pixelplumbing.com/ 中文文档地址:yunlzhang.github.io/sharp-docum…