I'm trying to update the value of a field for a specific row in MySQL but I'm getting an error that I don't really understand.
Here we have two tables CUSTOMER_TBL and ORDERS_TBL, and I want to update CUSTOMER_TBL so that the CUST_NAME for the customer who made the order with ORD_NUM equal to 23E934 is 'DAVIDS MARKET'.
Here are the two tables:
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)
And the code that gives the error:
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
What's the issue with referring to CUSTOMER_TBL in the subquery, and how would I get around this?
Thanks.
 
    