我正在尝试更新MySQL中特定行的字段值,但我收到的错误是我真的不理解。
此处我们有两个表CUSTOMER_TBL
和ORDERS_TBL
,我想更新CUSTOMER_TBL
,以便CUST_NAME
为使用{{1}订单的客户等于23E934是'DAVIDS MARKET'。
以下是两个表格:
ORD_NUM
提供错误的代码:
mysql> DESCRIBE CUSTOMER_TBL;
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| CUST_ID | varchar(10) | NO | PRI | NULL | |
| CUST_NAME | varchar(30) | NO | | NULL | |
| CUST_ADDRESS | varchar(20) | NO | | NULL | |
| CUST_CITY | varchar(15) | NO | | NULL | |
| CUST_STATE | char(2) | NO | | NULL | |
| CUST_ZIP | int(5) | NO | | NULL | |
| CUST_PHONE | char(10) | YES | | NULL | |
| CUST_FAX | varchar(10) | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
8 rows in set (0.05 sec)
mysql> DESCRIBE ORDERS_TBL;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| ORD_NUM | varchar(10) | NO | PRI | NULL | |
| CUST_ID | varchar(10) | NO | | NULL | |
| PROD_ID | varchar(10) | NO | | NULL | |
| QTY | int(6) | NO | | NULL | |
| ORD_DATE | date | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
5 rows in set (0.07 sec)
在子查询中引用mysql> UPDATE CUSTOMER_TBL
-> SET CUST_NAME = 'DAVIDS MARKET'
-> WHERE CUST_ID = (SELECT C.CUST_ID
-> FROM CUSTOMER_TBL C,
-> ORDERS_TBL O
-> WHERE C.CUST_ID = O.CUST_ID
-> AND O.ORD_NUM = '23E934');
ERROR 1093 (HY000): You can't specify target table 'CUSTOMER_TBL' for update in FROM clause
有什么问题,我该如何解决这个问题?
感谢。
答案 0 :(得分:1)
您可以执行以下操作:
UPDATE CUSTOMER_TBL INNER JOIN ORDERS_TBL
ON CUSTOMER_TBL.CUST_ID = ORDERS_TBL.CUST_ID
SET CUSTOMER_TBL.CUST_NAME = 'DAVIDS MARKET'
WHERE ORDERS_TBL.ORD_NUM = '23E934';