0

我无法通过选择表中已有的最高 final_id 并添加 +1 来更新 final_id。

下面的查询输出错误:“您无法在 FROM 子句中指定目标表 'customer_orders' 进行更新”,很遗憾我不明白为什么..

UPDATE customer_orders 
  SET final_id = (SELECT final_id FROM customer_orders ORDER BY final_id DESC)+1, 
      status = 2, 
      payment_id = '{$transaction_id}', 
      payment_type = '{$type}', 
      payment_reserved = '{$amount}', 
      payment_currency = '{$cur}', 
      payment_cardnopostfix = '{$postfix}', 
      payment_fraud_suspicious = '{$fraud}' 
  WHERE id = '{$order_id}'

我正在尝试为系统中的最终订单设置一个唯一递增的 ID。

我希望有人能告诉我我做错了什么!

最好的祝福

4

3 回答 3

1

您可以重写查询并使用 join

UPDATE customer_orders 
INNER JOIN (SELECT IFNULL(MAX(final_id),0) as max_id FROM customer_orders)a ON(1=1)
SET final_id = a.max_id+1, status = 2, payment_id = '{$transaction_id}', 
payment_type = '{$type}', payment_reserved = '{$amount}', 
payment_currency = '{$cur}', payment_cardnopostfix = '{$postfix}',
payment_fraud_suspicious = '{$fraud}' 
WHERE id = '{$order_id}'
于 2011-03-31T15:13:22.333 回答
0

将内部查询更改为SELECT max(final_id) FROM customer_orders

于 2011-03-31T15:08:31.067 回答
0

试试这个:

 UPDATE customer_orders        
    SET final_id = MT.MaxId + 1           -- use the computed max id, and increment
      , status = 2
      , payment_id = '{$transaction_id}'
      , payment_type = '{$type}'
      , payment_reserved = '{$amount}'
      , payment_currency = '{$cur}'
      , payment_cardnopostfix = '{$postfix}'
      , payment_fraud_suspicious = '{$fraud}'
   FROM customer_orders
      -- include a subquery to determine the max id from the customer_orders table
      -- and assign 'MT' as the name of the results table
      , (SELECT MAX(final_id) as MaxId FROM customer_orders) MT
  WHERE id = '{$order_id}'
于 2011-03-31T15:10:42.383 回答