在有些时候有级联关系的数据放在了同一张表中,在写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. 小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_1、SpringBoot2.x课程介绍和高手系列知识点

    1 ======================1.零基础快速入门SpringBoot2.0 5节课 =========================== 1.SpringBoot2.x课程全套介绍 ...

  2. un-资源-开源-WebGallery:Windows Web App Gallery

    ylbtech-资源-开源-WebGallery:Windows Web App Gallery Windows Web App Gallery 1.返回顶部   2.返回顶部   3.返回顶部   ...

  3. 为什么vue组件的属性,有的需要加冒号“:”,有的不用?

    https://segmentfault.com/q/1010000010929963/a-1020000010930077 <tab :line-width="2" act ...

  4. MyBatis的学习总结:调用存储过程【参考】

    一.创建存储过程 存储过程的目的:统计edi_test_task 正在运行的任务和非运行的任务 CREATE DEFINER=`root`@`%` PROCEDURE `edihelper`.`SP_ ...

  5. 【Leetcode_easy】852. Peak Index in a Mountain Array

    problem 852. Peak Index in a Mountain Array solution1: class Solution { public: int peakIndexInMount ...

  6. linux系统 重启盘符错乱问题

    linux磁盘重启乱序问题处理 最近到客户那去巡检时,客户提到一个问题,他们的rac在重启的时候,原来的sda1.sdb1.sdc1会对应变成sdd1.sde1.sdf1,由于他们使用的是盘符来绑定裸 ...

  7. 【转载】我为什么放弃了 Linux 内核学习?

    最近学习内核很是心累,碰巧看到这篇文章,有点意思~ 转自http://happypeter.github.io/index.html,以下为原文内容: 我在国内的技术类网站 / 博客上, 时常会看到分 ...

  8. IDEA将Git本地仓库Push至远程仓库

    转自:https://blog.csdn.net/qq_15653601/article/details/79870996 本地本地仓库项目: 配置Git自动识别本机Git配置

  9. windows下大数据开发环境搭建(4)——Spark环境搭建

    一.所需环境 · Java 8 · Python 2.6+ · Scala · Hadoop 2.7+ 二.Spark下载与解压 http://spark.apache.org/downloads.h ...

  10. Java program to find the largest element in array

    Java program to find the largest element in array Given an array of numbers, write a java program to ...