You can't specify target table 'tablename' for update in FROM clause的解决方法

简介:

在执行下面的sql时报错

 
  1. DELETE FROM temp WHERE id IN (SELECT a.id FROM t_user t JOIN temp a ON t.email = a.email); 

这种写法在sqlserver或者oracle中是支持的,但是mysql目前是不支持的,在mysql中不能同时查询一个表的数据再同时进行删除,只能用以下的办法折中

 
  1. CREATE TABLE tmp AS SELECT a.id FROM t_user t JOIN temp a ON t.email = a.email;  
  2. DELETE FROM temp WHERE id IN (SELECT id FROM tmp); 

先将查询到的数据放到一个临时表中,然后再对临时表进行查询,然后再删除目标表中满足条件的数据。


本文转自sucre03 51CTO博客,原文链接:http://blog.51cto.com/sucre/629493,如需转载请自行联系原作者

相关文章
|
6月前
|
SQL Oracle 关系型数据库
【MySQL异常】1093 - You can‘t specify target table ‘daily_job‘ for update in FROM clause
【MySQL异常】1093 - You can‘t specify target table ‘daily_job‘ for update in FROM clause
72 0
|
9月前
|
关系型数据库 MySQL
【问题处理】—— Mysql : You can‘t specify target table for update in FROM clause
【问题处理】—— Mysql : You can‘t specify target table for update in FROM clause
315 1
|
4月前
|
SQL 关系型数据库 MySQL
INSERT ... ON DUPLICATE KEY UPDATE Statement
INSERT ... ON DUPLICATE KEY UPDATE Statement
34 0
|
5月前
|
Oracle 关系型数据库 MySQL
MySQL语句执行报错You can‘t specify target table for update in FROM clause
MySQL语句执行报错You can‘t specify target table for update in FROM clause
50 0
|
7月前
|
存储 SQL 关系型数据库
1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column的解决办法
1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column的解决办法
87 0
|
关系型数据库 MySQL
Mysql 建表时报错 invalid ON UPDATE clause for 'create_date' column
原文:Mysql 建表时报错 invalid ON UPDATE clause for 'create_date' column 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.
3228 0
|
SQL 关系型数据库 MySQL
mysql操作中 出现You can‘t specify target table for update in FROM clause错误的解决方法
这个错误实际上也不能称之为咱们sql语句写的不行,实际上是我们在一些细节上没有遵循mysql的语法规范。 问题所在:我们一个sql语句中先select这个表,然后再update这个表的内容。 错误示范: UPDATE StuCose SET Grade=60 WHERE Sno IN( SELECT Sno FROM stucose WHERE Grade<=ALL( SELECT MIN(Grade) FROM stucos
254 0
mysql操作中 出现You can‘t specify target table for update in FROM clause错误的解决方法
|
关系型数据库 MySQL 数据库
MySQL - Column 'id' in where clause is ambiguous
MySQL - Column 'id' in where clause is ambiguous
588 0
|
SQL 关系型数据库 MySQL
Mysql - You can't specify target table '表名' for update in FROM clause 错误解决办法
Mysql - You can't specify target table '表名' for update in FROM clause 错误解决办法
253 0
Mysql - You can't specify target table '表名' for update in FROM clause 错误解决办法
Unsafe query: ‘Update‘ statement without ‘where‘ updates all table rows at once
Unsafe query: ‘Update‘ statement without ‘where‘ updates all table rows at once
606 0