在使用My Sql數據庫語法操做update時,第一時間想到的是一下寫法:數據庫
UPDATE purchase_request_detail SET convert_to_voucher_id=2, convert_to_voucher_type='inventory-voucher' WHERE detail_id IN (select detail_id from purchase_request_detail where request_id=1 and item_id=1) ;
可是這個時候就會報錯:You can't specify target table 'xxx' for update in FROMspa
My Sql的update的一些特色code
一、update 時,更新的表不能在set和where中用於子查詢;blog
二、update 時,能夠對多個表進行更新(Sql Server不行);ci
如:update table_a A,table_b B set A.B_ID=B.ID ,B.A_ID=A.ID; get
三、update 後面能夠作任意的查詢,這個做用等同於FROM;it
因此My Sql update是不容許使用子查詢的,正確寫法是:table
UPDATE purchase_request_detail AS table_1 INNER JOIN (select detail_id from purchase_request_detail where request_id=1 and item_id=1) AS table_2 SET convert_to_voucher_id=2, convert_to_voucher_type='inventory-voucher' WHERE table_1.detail_id = table_2.detail_id;