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. CTS camera的基础操作和debug

    手机端 设置永久不锁屏 1 CTS 进入cts目录tools 运行以下命令 ./cts-tradefed adb devices找设备数串 整跑 run cts -m CtsCameraTestCas ...

  2. Element 文档中的 Markdown 解析

    Element 的文档站是讲Markdown解析成vue组件在页面中渲染出来,转换过程如下图所示: 红框部分势必要对 Markdown 进行特殊的订制,订制过的 Markdown 像下面这样. ::: ...

  3. 《C++ Primer》笔记 第11章 关联容器

    关联容器类型 解释 按关键字有序保存元素 -- map 关联数组:保存关键字-值对 set 关键字即值,即只保存关键字的容器 multimap 关键字可重复出现的map multiset 关键字可重复 ...

  4. XAPKInstaller - XAPK游戏包安装器

    XAPKInstaller 一个用于安装XAPK游戏包的安装器. 程序需要读写存储与获取已安装应用权限才可正常运行. 长按条目可显示文件的详细信息. SDK小于24(Android N)的设备会显示应 ...

  5. 微信小程序折线图表折线图加区域图

    1.先来个效果图 这里我用的是插件@antv/f2-canvas(安装的方法如下) npm init 此处如果直接使用官方npm install 可能会出现没有node_modules错误 npm i ...

  6. C语言相关的基础字符串函数

    C语言中没有专门的字符串类型,所以就用字符数组和字符指针形式表示 1 char arr[]="abcdef"; //字符数组表示的字符串 2 char*arr="abce ...

  7. LeetCode-二叉搜索树的范围和

    二叉搜索树的范围和 LeetCode-938 首先需要仔细理解题目的意思:找出所有节点值在L和R之间的数的和. 这里采用递归来完成,主要需要注意二叉搜索树的性质. /** * 给定二叉搜索树的根结点 ...

  8. linux时间问题

    如果遇到创建时间和更新时间,不一致,先将时间调整为一致. 导致不一致的原因可能是时区不对,使用 tzselect ,将时区调整为Asia/Shanghai , cp /usr/share/zonein ...

  9. WPF 基础 - 点击事件的执行顺序及 Button 点击事件的特殊性

    1. 点击事件的执行顺序 PreviewMouseLeftButtonDown PreviewMouseDown MouseLeftButtonDown MouseDown PreviewMouseL ...

  10. Intellij IDEA maven设置tomcat

    1 pom.xml配置插件 <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId&g ...