mysql中You can't specify target table for update in FROM clause错误的意思是说,不能先select出同一表中的某些值,再update这个表(在同一语句中)。

例①如下面这个sql:

代码如下:

delete from tbl where id in  

        select max(id) from tbl a where EXISTS 
        ( 
            select 1 from tbl b where a.tac=b.tac group by tac HAVING count(1)>1 
        ) 
        group by tac 

改写成下面就行了:

代码如下:

delete from tbl where id in  

    select a.id from  
    ( 
        select max(id) id from tbl a where EXISTS 
        ( 
            select 1 from tbl b where a.tac=b.tac group by tac HAVING count(1)>1 
        ) 
        group by tac 
    ) a 

也就是说将select出的结果再通过中间表select一遍,这样就规避了错误。

注意,这个问题只出现于mysql,mssql和oracle不会出现此问题。

You can't specify target table for update in FROM clause含义:不能在同一表中查询的数据作为同一表的更新数据。

例②:

我想查询t_user_asset的余额加上50000作为更新字段f_cashAmount的值,这样写是不行的

  1. UPDATE t_user_asset SET f_cashAmount =
  2. (
  3. SELECT (ua.f_cashAmount+50000) cashAmount FROM t_user_asset ua WHERE ua.f_userId = 290
  4. )
  5. WHERE f_userId = 290

修改成以下写法就行,意思就是变个方向,在select外边套一层,让数据库认为你不是查同一表的数据作为同一表的更新数据:

  1. UPDATE t_user_asset SET f_cashAmount =
  2. (
  3. SELECT ub.cashAmount FROM
  4. (
  5. SELECT (ua.f_cashAmount+50000) cashAmount FROM t_user_asset ua WHERE ua.f_userId = 290
  6. ) ub
  7. )
  8. WHERE f_userId = 290

以上问题只针对mysql数据库

例③:https://pintia.cn/problem-sets/1375423976918028288/problems/1375424236121960453

mysql下错解:

update orderdetails
set unitprice = unitprice - 1
where Quantity in
(select min(Quantity)from orderdetails)

正解:

update orderdetails
set unitprice = unitprice - 1
where Quantity in
(select av from (select min(Quantity) av from orderdetails)as aa)

解决mysql You can't specify target table for update in FROM clause错误的更多相关文章

  1. mysql You can't specify target table for update in FROM clause解决方法

    mysql You can't specify target table for update in FROM clause解决方法出现这个错误的原因是不能在同一个sql语句中,先select同一个表 ...

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

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

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

  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 for update in FROM clause 这个错误的意思是不能在同一个sql语句中,先select同一个表的某些值 ...

  7. mysql 出现You can't specify target table for update in FROM clause错误的解决方法

    mysql出现You can’t specify target table for update in FROM clause 这个错误的意思是不能在同一个sql语句中,先select同一个表的某些值 ...

  8. mysql中You can't specify target table for update in FROM clause错误

    原SQL delete from DEP_SYSTEM_PORTLET_SETTINGS where ID in ( select ID from DEP_SYSTEM_PORTLET_SETTING ...

  9. MySQL之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 c ...

随机推荐

  1. SpringBoot文件上传与POI的使用

    1.使用springboot上传文件 本文所要源码在一个项目中,源码:https://github.com/zhongyushi-git/springboot-upload-download.git. ...

  2. Python网络编程相关的库与爬虫基础

    PythonWeb编程 ①相关的库:urlib.urlib2.requests python中自带urlib和urlib2,他们主要使用函数如下: urllib: urlib.urlopen() ur ...

  3. 设置ViewPager 自动滑动时间,速度 方便展示动画

    ViewPager.setCurrentItem(position),即使已设置动画,但是没有动画效果 原因:因为ViewPager滑动之前的时间间隔太短,可以通过反射,去修改ViewPager自动滑 ...

  4. 报错: You are using pip version 10.0.1, however version 18.0 is available.

    报错: You are using pip version 10.0.1, however version 18.0 is available. You should consider upgradi ...

  5. 后端程序员之路 40、Pthreads

    POSIX线程(POSIX threads),简称Pthreads,是线程的POSIX标准.线程这个东西在操作系统原理里讲得比较清楚了,再加上对windows那一套进程线程的东西比较清楚,所以这里还是 ...

  6. Markdown基础使用方法

    Markdown基础使用方法 标题的几种用法 * 选中标题(Ctrl+1~Crtl+6),分别为标题1-6.* #+空格+内容 为一级标题##+空格+内容为二级标题:以此类推. 字体快捷键及使用方法 ...

  7. POJ-3436(网络流+最大流+输出路径)

    ACM Computer Factory POJ-3436 题目就是一个工厂n个加工机器,每个机器有一个效率w,q个材料入口,q个材料出口,每个口有三个数表示状态,1表示一定有入/出的材料,0表示没有 ...

  8. mysql内一些可以报错注入的查询语句

        一.exp() 取反参数 该函数简单来说就是,以e为底的对数,在当传递一个大于709的值时,函数exp()就会引起一个溢出错误,取反则可以导致很小的数值变得很大,比如说0 这样既可配合使用,e ...

  9. 通达OA 任意文件上传-2013/2015版本

    参考 http://wiki.0-sec.org/0day/%E9%80%9A%E8%BE%BEoa/11.html 影响版本 2013版本 2015版本 漏洞文件 general/vmeet/wbU ...

  10. 漏洞复现-CVE-2017-4971-Spring Web Flow 远程代码执行

            0x00 实验环境 攻击机:Win 10 靶机也可作为攻击机:Ubuntu18 (docker搭建的vulhub靶场)(兼顾反弹shell的攻击机) 0x01 影响版本 Spring ...