mysql中You can't specify target table for update in FROM clause错误
2016-04-28 10:38
447 查看
MySQL中You can't specify target table <tbl> 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不会出现此问题。
顶
2
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不会出现此问题。
顶
2
相关文章推荐
- MySql 比Replace Into更适合的用法,外加SqlServer的方式。
- mysql非安装版配置
- MySQL存储过程实现动态执行SQL
- 打开MySQL数据库远程访问的权限
- mysql 触发器不能创建表
- 解决MySQL时间转换异常
- mysql数据库管理工具(navicat for mysql) 10.1.7 绿色中文版
- mysql5.5 主从复制 (触发器,函数,存储引擎,事件处理)说明
- 查看mysql语句运行时间的2种方法
- Mysql (三) - 视图、字符集、校对集、触发器
- mysql的下载到安装使用
- mysql ab复制
- 一、安装mysql、mysql基础、mysql常用命令、数据表以及数据类型
- MySQL5.6安装脚本
- mysql:约束
- MySQL数据备份之mysqldump使用
- windows mysql 自动备份的几种方法
- 在MySQL存储过程的语句中有三个标准的循环方式
- CI 配置 新浪云mysql数据库
- MySQL语句大全:创建、授权、查询、修改等