2015-12-23 106 views
1

我正在編寫更新當前記錄的SQL語句。我遇到的問題是,如果其中一列具有特定的值,那麼我不希望該值被覆蓋。如果表中狀態列的值等於Received,我不希望它被覆蓋。 這裏是我的示例代碼:使用重複鍵更新的條件

INSERT INTO transfer_order (invoice_id, contact_id, ship_date, ship_info, tracking_number, date_created, date_modified, account_number, shipping, discount, status) 
VALUES ('$invoice_id', $contact_id, $ship_date, $shipper_id, $tracking_number , $date_created, $date_modified, $account_name, $shipping_cost, $discount, $status) ON DUPLICATE KEY UPDATE contact_id=VALUES(contact_id), 
ship_date=VALUES(ship_date), tracking_number=VALUES(tracking_number), 
ship_info=VALUES(ship_info), date_created=VALUES(date_created), 
date_modified=VALUES(date_modified),shipping=VALUES(shipping),status=VALUES(status) 

回答

0

你的意思是這樣的?

INSERT INTO transfer_order 
(invoice_id, contact_id, ship_date, ship_info, tracking_number, date_created, date_modified, account_number, shipping, discount, status) 
VALUES ON DUPLICATE KEY UPDATE 
contact_id = CASE status 
       WHEN 'Received' THEN VALUES(contact_id) 
      END , 
ship_date = CASE status 
       WHEN 'Received' THEN VALUES(ship_date) 
      END, 
tracking_number = VALUES(tracking_number), 
ship_info = VALUES(ship_info), 
date_created = VALUES(date_created), 
date_modified = VALUES(date_modified), 
shipping = VALUES(shipping), 
status = VALUES(status) 
+0

我試圖用您提供的代碼進行更改,它仍然覆蓋了以前的記錄。 – MatrixMan78