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. Pandas初体验

    目录 Pandas 一.简介 1.安装 2.引用方法 二.series 1.创建方法 2.缺失数据处理 2.1 什么是缺失值 2.2 NaN特性 2.3 填充NaN 2.4 删除NaN 2.5 其他方 ...

  2. 【Azure Redis 缓存】Azure Redis 功能性讨论二

    继承上一次讨论了Azure Redis的可用性,可靠性,稳定性,安全性,监控方面的九大功能点.详情可回顾文章:[Azure Redis 缓存]Azure Redis功能性讨论 这次我们继续讨论Azur ...

  3. HDOJ-1711(KMP算法)

    Number Sequence HDOJ-1711 1.这里使用的算法是KMP算法,pi数组就是前缀数组. 2.代码中使用到了一个技巧就是用c数组看成是复合字符串,里面加一个特殊整数位-1000006 ...

  4. 在Linux上从零开始部署前后端分离的Vue+Spring boot项目

    最近做了一个前后端分离的商城项目来熟悉开发的整个流程,最后希望能有个正式的部署流程,于是试着把项目放在云服务器上,做了一下发现遇到了不少问题,借此记录一下整个部署的过程. 使用的技术栈如标题所说大体上 ...

  5. apicloud打包的ios证书的获取方法

    apicloud云编译的时候,需要测试证书或者正式证书进行编译. 那么这个证书是怎么来的呢?通过什么渠道可以获取呢? 这里我介绍下使用香蕉云编这个在线工具来生成: 1.登录香蕉云编,生成证书的csr文 ...

  6. POJ_1273 Drainage Ditches 【网络流】

    一.题面 Drainage Ditches 二.分析 网络流的裸题. 1 Edmonds-Karp算法 求解网络流其实就是一个不断找增广路,然后每次找到一条增广路后更新残余网络的一个过程. EK算法主 ...

  7. Vue中去除文本框回车默认事件

    使用v-on:keydown.enter.prevent即可 <input type="password" class="form-control" id ...

  8. Ubuntu20.04linux内核(5.4.0版本)编译准备与实现过程-编译过程(2)

    前面因为博客园维修,所以内核编译过程一直没有发出来,现在把整个内核过程分享出来.本随笔给出内核的编译实现过程,在编译前需要参照我前面一篇随笔: Ubuntu20.04linux内核(5.4.0版本)编 ...

  9. 从新建文件夹开始构建UtopiaEngine(1)

    序言 在苦等了半年多之后,我终于开始了向往已久的实时NPR游戏引擎项目--Utopia Engine,这半年多一直为了构建这个引擎在做很多准备:多线程.动态链接库.脚本引擎.立即渲染GUI--统统吃了 ...

  10. SSM 电影后台管理项目

    SSM 电影后台管理项目 概述 通过对数据库中一张表的CRUD,将相应的操作结果渲染到页面上. 笔者通过这篇博客还原了项目(当然有一些隐藏的坑),然后将该项目上传到了Github.Gitee,在末尾会 ...