mysql中You can’t specify target table for update in FROM clause错误解决方法

作者:junjie 时间:2024-01-19 02:24:26 

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
0
投稿

猜你喜欢

  • Python混合使用同步和异步函数的方法

    2021-07-19 05:01:05
  • Python操作PDF文件之实现A3页面转A4

    2021-03-06 19:45:06
  • Mysql锁机制之行锁、表锁、死锁的实现

    2024-01-26 08:29:43
  • python用类实现文章敏感词的过滤方法示例

    2022-04-10 19:30:25
  • vue中的provide/inject的学习使用

    2024-04-27 16:08:55
  • 数据库名词解释

    2008-09-12 17:28:00
  • Python中免验证跳转到内容页的实例代码

    2021-03-27 11:19:55
  • Python3下错误AttributeError: ‘dict’ object has no attribute’iteritems‘的分析与解决

    2023-04-21 05:52:45
  • python如何实现代码检查

    2022-07-22 18:19:12
  • 以Python的Pyspider为例剖析搜索引擎的网络爬虫实现方法

    2022-02-15 19:11:48
  • 详解Python中的null是什么

    2022-11-30 15:33:56
  • (X)HTML Strict 下的嵌套规则

    2008-03-08 18:39:00
  • JS实现css边框样式设置工具

    2008-05-25 16:22:00
  • 使用cx_freeze把python打包exe示例

    2021-06-10 05:36:20
  • vue中项目页面空白但不报错产生的原因及分析

    2024-05-03 15:12:17
  • python多线程互斥锁与死锁

    2023-11-10 14:16:28
  • 使用matplotlib绘制并排柱状图的实战案例

    2022-04-06 01:22:25
  • sqlserver四舍五入使用round函数及cast和convert函数

    2024-01-21 18:20:53
  • Python Flask框架开发之运用SocketIO实现WebSSH方法详解

    2021-11-06 01:03:33
  • 使用CSS简单实现垂直居中

    2008-06-23 07:32:00
  • asp之家 网络编程 m.aspxhome.com