这篇文章主要介绍了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这个表(在同一语句中)。 例如下面这个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的值,这样写是不行的。

[sql] view plain copy

  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外边套一层,让数据库认为你不是查同一表的数据作为同一表的更新数据:

[sql] view plain copy

  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数据库

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错误的意思是说,不能先select出同一表中的某些值,再update这个表( ...

  2. MYSQL 1093 之You can't specify target table for update in FROM clause解决办法

    You can't specify target table for update in FROM clause含义:不能在同一表中查询的数据作为同一表的更新数据. 出现以上错误,是因为想将表自身的字 ...

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

  4. 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同一个表的某些值 ...

  5. mysql error: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错误的意思是说,不能先select出同一表中的某些值,再update这个表(在同一语句中)。

    将select出的结果再通过中间表select一遍,这样就规避了错误.注意,这个问题只出现于mysql,mssql和oracle不会出现此问题. mysql中You can't specify tar ...

  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错误的解决方法

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

  9. mysql中You can't specify target table for update in FROM clause

    使用mysql在删除表中重复记录 delete from user where username in (select user name form(select username from user ...

随机推荐

  1. IDEA设置

    一:代码提示 二:自动导入

  2. String.IsNullorEmpty()方法的使用

    != null 就是不为null !string.IsNullOrEmpty  不是null且不是""(string.Empty)

  3. 【nodejs】--express的中间件multer实现图片文件上传--【XUEBIG】

    Multer是nodejs中处理multipart/form-data数据格式(主要用在上传功能中)的中间件.该中间件不处理multipart/form-data数据格式以外的任何形式的数据 Tips ...

  4. ps选区的两种复制方法

    1.选区选中之后,利用移动工具,按住alt键,拖动即可复制所选区域. ps:再一个图层上操作. 2.选区选中之后,Ctrl+c .Ctrl+v复制粘贴,按Ctrl+T移动. ps:新建一个图层操作,不 ...

  5. ISP PIPLINE(零) 知识综述预热

    本文为camera isp pipline概述 ISP,即image signal processing.为图像成型做的处理工作.适应不同光学环境下图像的还原. pipline流程如下: 光通过LEN ...

  6. js发送邮件 不会调用客户端。

    方式三:使用node中nodemail 首先需要安装node的环境,然后安装nodemailer: npm install nodemailer --save npm install nodemail ...

  7. JN5139 zigbee 资料

    JN5139模块是一系列可以使使用者在最短的时间内在最低的成本下实现IEEE802.15.4或ZigBee兼容系统的表贴模块.此款模块减少了用户对于RF板设计和测试框架的昂贵漫长的开发时间.这些模块利 ...

  8. 潭州课堂25班:Ph201805201 爬虫高级 第十一课 Scrapy-redis分布 项目实战 (课堂笔

  9. 以加密方式访问tomcat服务器

    以加密方式(https)访问tomcat服务器,就要求对相应的端口配置加密连接器connector. 配置加密连接器的方法如下: 1.生成数字证书 利用jdk里的keytool生成证书,keytool ...

  10. setuid setgid

    http://blog.csdn.net/oo__yan/article/details/7076889 在Linux系统中每个普通用户都可以更改自己的密码,这是合理的设置. 问题是:用户的信息保存在 ...