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

MySQL出現You can’t specify target table for update in FROM clause 這個錯誤的意思是不能在同一個sql語句中,先select同一個表的某些值,然後再update這個表。 

例如:message表儲存了多個使用者的訊息

建立表

CREATE TABLE `message` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `uid` int(10) unsigned NOT NULL,
 `content` varchar(255) NOT NULL,
 `addtime` datetime NOT
NULL, PRIMARY KEY (`id`), KEY `uid` (`uid`), KEY `addtime` (`addtime`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

插入資料

insert into message(uid,content,addtime) values
(1,'content1','2016-09-26 00:00:01'),
(2,'content2','2016-09-26 00:00:02'),
(3,'content3','2016-09-26 00:00:03'),
(1,'content4','2016-09-26 00:00:04'),
(3,'content5'
,'2016-09-26 00:00:05'), (2,'content6','2016-09-26 00:00:06'), (2,'content7','2016-09-26 00:00:07'), (4,'content8','2016-09-26 00:00:08'), (4,'content9','2016-09-26 00:00:09'), (1,'content10','2016-09-26 00:00:10');

表結構及資料如下:

mysql> select * from message;
+----+-----+-----------+---------------------+
| id | uid | content   | addtime             |
+----+-----+-----------+---------------------+
| 1 | 1 | content1 | 2016-09-26 00:00:01 | | 2 | 2 | content2 | 2016-09-26 00:00:02 | | 3 | 3 | content3 | 2016-09-26 00:00:03 | | 4 | 1 | content4 | 2016-09-26 00:00:04 | | 5 | 3 | content5 | 2016-09-26 00:00:05 | | 6 | 2 | content6 | 2016-09-26 00:00:06 | | 7 | 2 | content7 | 2016-09-26 00:00:07 | | 8 | 4 | content8 | 2016-09-26 00:00:08 | | 9 | 4 | content9 | 2016-09-26 00:00:09 | | 10 | 1 | content10 | 2016-09-26 00:00:10 | +----+-----+-----------+---------------------+ 10 rows in set (0.00 sec)

然後執行將每個使用者第一條訊息的內容更新為Hello World

mysql> update message set content='Hello World' where id in(select min(id) from message group by uid);
ERROR 1093 (HY000): You can't specify target table 'message' for update in FROM clause

因為在同一個sql語句中,先select出message表中每個使用者訊息的最小id值,然後再更新message表,因此會出現 ERROR 1093 (HY000): You can’t specify target table ‘message’ for update in FROM clause 這個錯誤。 


解決方法:select的結果再通過一箇中間表select多一次,就可以避免這個錯誤

update message set content='Hello World' where id in( select min_id from ( select min(id) as min_id from message group by uid) as a );

執行:

mysql> update message set content='Hello World' where id in( select min_id from ( select min(id) as min_id from message group by uid) as a );
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4  Changed: 4  Warnings: 0

mysql> select * from message;
+----+-----+-------------+---------------------+
| id | uid | content     | addtime             |
+----+-----+-------------+---------------------+
|  1 |   1 | Hello World | 2016-09-26 00:00:01 |
|  2 |   2 | Hello World | 2016-09-26 00:00:02 |
|  3 |   3 | Hello World | 2016-09-26 00:00:03 |
|  4 |   1 | content4    | 2016-09-26 00:00:04 |
|  5 |   3 | content5    | 2016-09-26 00:00:05 |
|  6 |   2 | content6    | 2016-09-26 00:00:06 |
|  7 |   2 | content7    | 2016-09-26 00:00:07 |
|  8 |   4 | Hello World | 2016-09-26 00:00:08 |
|  9 |   4 | content9    | 2016-09-26 00:00:09 |
| 10 |   1 | content10   | 2016-09-26 00:00:10 |
+----+-----+-------------+---------------------+
10 rows in set (0.00 sec)

注意,只有mysql會有這個問題,mssql與Oracle都沒有這個問題。

相關推薦

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同一個表的某些值,然後再update這個表。 例如:message表儲存了多個使用者的訊息 建立表 CREATE TABLE

MYSQLYou can't specify target table for update in FROM clause解決辦法

這篇文章主要介紹了mysql中You can’t specify target table for update in FROM clause錯誤解決方法,需要的朋友可以參考下 mysql中You can't specify target table for update

如何解決You cant 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 target_info_day where id

按條件刪除記錄時報You cant specify target table for update in FROM clause錯誤解決方法(寫於20161107)

核心概念:     mysql中,不能先select一個表的記錄,在按此條件進行更新和刪除同一個表的記錄。解決辦法是,將select得到的結果,再通過中間表select一遍,這樣就規避了錯誤,這個問題只出現於mysql,mssql和oracle不會出現此問題。自己例項一:如下業務場景,ecs_order_sh

MySQLYou can't specify target table for update in FROM clause

問題:You can't specify target table for update in FROM clause 含義:不能在同一表中查詢的資料作為同一表的更新資料。 注意:這個問題只出現於mysql,mssql和oracle不會出現此問題。 delete from people wher

mysql修改刪除You can't specify target table for update in FROM clause的問題

you div code sql 語句 操作 查詢 重復數 -c sele 表中出現重復數據,需要刪除重復數據,只保留一條 DELETE FROM crm_participant WHERE id IN ( SELECT c.id cid F

MySQLYou can't specify target table '表名' for update in FROM clause錯誤

在MySQL中,寫SQL語句的時候 ,可能會遇到You can't specify target table '表名' for update in FROM clause這樣的錯誤,它的意思是說,不能先select出同一表中的某些值,再更新這個表(在同一語句中),即不能依據某

MySQLYou can't specify target table '表名' for update in FROM clause錯誤解決辦法

在MySQL中,寫SQL語句的時候 ,可能會遇到You can't specify target table '表名' for update in FROM clause這樣的錯誤,它的意思是說,不能先select出同一表中的某些值,再update這個表(在同一語句中),即

MySQL 錯誤碼: 1093 You can't specify target table 'jc_user' for update in FROM clause

MySQL 錯誤碼: 1093 You can’t specify target table ‘jc_user’ for update in FROM clause bug如何出現及解決方案 根據錯誤資訊可知:不能查詢一張表的同時修改同一張表

MySQL:You cant specify target table ‘A’ for update in FROM clause

按照MYSQL5.0文件的解釋:我們不能在修改表A的同時在其子查詢中使用到表A,但是可以通過在子查詢中在巢狀一層針對表A的子查詢,因為最裡層的子查詢產生的結果存在臨時表中,與表A沒有關係。 解決方法:把類似於 UPDATE t ... WHERE col = (SELE

關於mysql 5.7版本“報[Err] 1093 - You can't specify target table 'XXX' for update in FROM clause”錯誤的bug

title _id fma xxx tps ice sql each targe 不同於oracle和sqlserver,mysql並不支持在更新某個表的數據時又查詢了它,而查詢的數據又做了更新的條件,因此我們需要使用如下的語句繞過: UPDATE teaching_de

You can't specify target table 't_mail_marketing' for update in FROM clause

update in table use pre RKE can stat mail date update t_mail_marketing set `STATUS` = 1 where ID in ( select b.PARENT_ID from (SELECT D

You can't specify target table 'table' for update in FROM clause

tar bsp stock select CA HA lec pan having delete from table1 where ID not in(select max(ID) ID from table1 group by row1) and row1 in

[Err] 1093 - You can't specify target table 'master_data' for update in FROM clause

sel master mce 沒有 AR delete 查詢 數據 select delete from master_data where category_id not in (select category_id from master_data a, bc_cate

You can't specify target table 't_open_user' for update in FROM clause

https://blog.csdn.net/jiangyu1013/article/details/79108498 報錯如題,意思大致是:在一條 sql 語句中不能先查出來部分內容,再同時又對當前表作修改。 解決方法:給查詢加別名,用中間表來實現不是對同一表作操作。 如錯誤定法

錯誤:You can't specify target table 'xxx' for update in FROM clause的解決

參考:https://www.cnblogs.com/pcheng/p/4950383.html   解決:   程式碼: <!-- 執行"取消收藏" 操作 -> 根據前端傳入的商品id和sessio

[Err] 1093 - You can't specify target table 's1_test' for update in FROM clause

前提說明:資料庫採用的是mysql。 資料庫表格: 題目: 刪除除了編號不同,其他資訊都相同的冗餘資訊。 思路: 1.找出除了編號不同,其他資訊不全相同的編號。 關鍵詞:group by……having :分組查詢,我對這個關鍵詞的理解是:不同的行之間找出列相同的一項或者

updateupdate中無法用基於被更新表的子查詢,You can't specify target table 'test1' for update in FROM clause.

子查詢 src nbsp spec tab can 技術分享 例如 bubuko update中無法用基於被更新表的子查詢,You can‘t specify target table ‘test1‘ for update in FROM clause. 情況如下: (

Navicat for Mysql 出現2003 can't connect to MySQL server on

                環境:Navicat for Mysql 8.2 + MySQL Sever 5.1問題:通過MySQL命令列能連線,但是通過Navicat for Mysql 8.2卻連不上,出現如下錯誤:2003 - can's connect to MySQL server on 'lo

MySQL解決報錯[Err] 1093 - You can't specify target...

做資料變更的時候經常遇到這樣的報錯 DELETE FROM ssq_contract_record WHERE LOAN_ID IN ( SELECT ssq_contract_record.LOAN_ID FROM ssq_contract_record