数据库

mysql中You can't specify target table for update in FROM clause错误

作者:admin 来源:web交流网 2018-09-30 11:38 浏览:0 我要评论(0)

mysql中You can’t specify target table for update in FROM clause错误的意思是说,不能先select出同一表中的某些值,再up

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不会出现此问题。
 

转载请注明出处。

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • MySQL错误中无法连接 MySQL server (10060)

    MySQL错误中无法连接 MySQL server (10060)

    2016-07-05

网友点评
评论(已有0条评论)
还没有评论,快来抢沙发吧!
新闻
  • 新闻
  • 软件
精彩导读