我有一个名为customers
的表,其主键字段为customerNumber
。客户1001需要将其customerNumber
更改为比具有最高customerNumber
的客户多一的值。
这是我尝试过的:
UPDATE customers
SET customerNumber = (SELECT (MAX(customerNumber) + 1)
FROM customers)
WHERE customerNumber = 1001;
但我得到
ERROR 1093 (HY000): You can't specify target table 'customers' for update in FROM clause
我在这里想念什么?
最佳答案
使用join
:
UPDATE customers c CROSS JOIN
(SELECT MAX(CustomerNumber) + 1 as newcn
FROM customers
) cmax
SET customerNumber = cmax.newcn
WHERE customerNumber = 1001;