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. Redis缓存穿透、缓存雪崩、缓存击穿好好说说

    前言 Redis是目前非常流行的缓存数据库啦,其中一个主要作用就是为了避免大量请求直接打到数据库,以此来缓解数据库服务器压力:用上缓存难道就高枕无忧了吗?no,no,no,没有这么完美的技术, 缓存穿 ...

  2. springboot源码(4)

    我们上3个篇章写了springboot的自动装配.servlet组件的注入以及web容器实现内嵌的原理,现在我们来看springboot启动过程中到底做了些什么,也就是打开我们的run方法: 这里我们 ...

  3. Serverless 2.0,鸡蛋还是银弹?

    简介: 本篇旨在介绍 Serverless 如今应用到应用(非病句)的各种困境,以及帮助用户如何去规避一些问题,提前了解方向. 浪潮 从 2014 年 Serverless 冒头至今,已经有无数的勇士 ...

  4. PAT-1064(Complete Binary Search Tree)JAVA实现

    Complete Binary Search Tree PAT-1064 本次因为涉及到完全二叉排序树,所以可以使用数组的形式来存储二叉排序树 对输入序列排序后,得到的是中序遍历二叉排序树的序列.对这 ...

  5. 在 .NET Core 中应用六边形架构

    在本文中,您会看到一个Web API应用的模板,在.NET Core 中应用了六边形架构,并且里面包含了一些基础功能. 介绍 这是一个模板项目,里面集成了一些必备的基础功能,当我们需要开发一个新项目时 ...

  6. RichTextBox FlowDocument类型操作

      最近研究微信项目,套着web版微信协议做了一个客户端,整体WPF项目MVVM架构及基本代码参考于:http://www.cnblogs.com/ZXdeveloper/archive/2016/1 ...

  7. WebRTC 音视频同步原理与实现

    所有的基于网络传输的音视频采集播放系统都会存在音视频同步的问题,作为现代互联网实时音视频通信系统的代表,WebRTC 也不例外.本文将对音视频同步的原理以及 WebRTC 的实现做深入分析. 时间戳 ...

  8. C语言入门--初来乍到

    Hi,我是fish-studio,这是我写的第一篇博客,接下来我会以萌新的角度来与大家一起学习C语言,我也不是什么大佬,在我写的教程中会尽量详细的把我遇到的问题写出来,也会结合一些网上的文章进行编写, ...

  9. ResNet的个人总结

    ResNet可以说是我认真读过的第一篇paper,据师兄说读起来比较简单,没有复杂的数学公式,不过作为经典的网络结构还是有很多细节值得深究的.因为平时不太读英文文献,所以其实读的时候也有很多地方不是很 ...

  10. 前端-CS-04

    一:DOM(文档对象模型) document 简写DOM 1.DOM中定义变量用 var  如下截图中:定义demo变量 2.取一个input输入框中的值的方法: 1)先如1中,在dom中顶一个一个变 ...