ERROR 1093 解决方法
生活随笔
收集整理的這篇文章主要介紹了
ERROR 1093 解决方法
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
對(duì)于這個(gè)錯(cuò)誤信息:
ERROR 1093 (HY000): You can't specify target table 'clients' for update in FROM clause
或者:ERROR 1093 (HY000): You can't specify target table 'clients' for delete in FROM clause
一直以來我以為只有一種辦法。不過今天翻開以前的書,發(fā)現(xiàn)還有一個(gè)方法。
表結(jié)構(gòu)和示例數(shù)據(jù):
mysql> show create table branches\G
*************************** 1. row ***************************
?????? Table: branches
Create Table: CREATE TABLE `branches` (
? `bid` int(11) NOT NULL,
? `cid` int(11) NOT NULL,
? `bdesc` varchar(1000) NOT NULL,
? `bloc` char(2) NOT NULL,
? PRIMARY KEY (`bid`),
? KEY `cid` (`cid`),
? CONSTRAINT `branches_ibfk_1` FOREIGN KEY (`cid`) REFERENCES `clients` (`cid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
mysql> select * from branches;
+------+-----+--------------------------------+------+
| bid? | cid | bdesc????????????????????????? | bloc |
+------+-----+--------------------------------+------+
| 1011 | 101 | Corporate HQ?????????????????? | CA?? |
| 1012 | 101 | Accounting Department????????? | NY?? |
| 1013 | 101 | Customer Grievances Department | KA?? |
| 1031 | 103 | N Region HO??????????????????? | ME?? |
| 1032 | 103 | NE Region HO?????????????????? | CT?? |
| 1033 | 103 | NW Region HO?????????????????? | NY?? |
| 1041 | 104 | Branch Office (East)?????????? | MA?? |
| 1042 | 104 | Branch Office (West)?????????? | CA?? |
| 1101 | 110 | Head Office??????????????????? | CA?? |
+------+-----+--------------------------------+------+
9 rows in set (0.00 sec)
mysql> show create table clients\G
*************************** 1. row ***************************
?????? Table: clients
Create Table: CREATE TABLE `clients` (
? `cid` int(11) NOT NULL,
? `cname` varchar(64) NOT NULL,
? PRIMARY KEY (`cid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
mysql> select * from clients;
+-----+-----------------------------+
| cid | cname?????????????????????? |
+-----+-----------------------------+
| 101 | JV Real Estate????????????? |
| 102 | ABC Talent Agency?????????? |
| 103 | DMW Trading???????????????? |
| 104 | Rabbit Foods Inc??????????? |
| 110 | Sharp Eyes Detective Agency |
+-----+-----------------------------+
5 rows in set (0.00 sec)
mysql> delete from clients where cid = (select clients.cid from clients left join branches using(cid) where bid is null);
ERROR 1093 (HY000): You can't specify target table 'clients' for update in FROM
clause
解決辦法
1、利用變量賦值。
mysql> select @m_cid:=clients.cid from clients left join branches using(cid) where bid is null;
+---------------------+
| @m_cid:=clients.cid |
+---------------------+
|???????????????? 102 |
+---------------------+
1 row in set (0.00 sec)
mysql> delete from clients where cid = 102;
Query OK, 1 row affected (0.05 sec)
mysql> select * from clients;
+-----+-----------------------------+
| cid | cname?????????????????????? |
+-----+-----------------------------+
| 101 | JV Real Estate????????????? |
| 103 | DMW Trading???????????????? |
| 104 | Rabbit Foods Inc??????????? |
| 110 | Sharp Eyes Detective Agency |
+-----+-----------------------------+
4 rows in set (0.00 sec)
2、用EXISTS關(guān)鍵字和相關(guān)子查詢:(不過這個(gè)沒有之前的效率高)
mysql> insert into clients values(102,'ABC Talent Agency');
Query OK, 1 row affected (0.05 sec)
mysql> delete from clients where not exists
??? -> (
??? -> select * from branches where branches.cid = clients.cid
??? -> );
Query OK, 1 row affected (0.06 sec)
mysql> select * from clients;
+-----+-----------------------------+
| cid | cname?????????????????????? |
+-----+-----------------------------+
| 101 | JV Real Estate????????????? |
| 103 | DMW Trading???????????????? |
| 104 | Rabbit Foods Inc??????????? |
| 110 | Sharp Eyes Detective Agency |
+-----+-----------------------------+
4 rows in set (0.00 sec)
轉(zhuǎn)載于:https://blog.51cto.com/wyfirst/195882
超強(qiáng)干貨來襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的ERROR 1093 解决方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux 2.6内核中新的锁机制--R
- 下一篇: javascript动态创建表格:新增、