2014-09-20 72 views
1

我使用MySQL 5.5,我有兩個表,inventory_items和menu_items。它們都有一個bin_number,它是一個字符串,而location_id是一個整數。兩個表的sql更新語句

menu_items 
invetory_item_id | location_id | bin_number 


inventory_items 
id | location_id | bin_number 

我想更新menu_items INVENTORY_ITEM_ID到inventory_items表,他們是在bin_number和LOCATION_ID平等的ID。我可以繼續通過每個這樣的:

update menu_items set inventory_item_id=(select id from inventory_items where 
bin_number='7060' and location_id=37) where bin_number='7060' and location_id=37; 

有沒有辦法說全部更新menu_items到了bin_number和LOCATION_ID是menu_items和inventory_items的一樣嗎?

THX

回答

1

您可以在UPDATE使用JOIN

UPDATE menu_items mi 
    JOIN inventory_items ii ON mi.bin_number=ii.bin_number 
     AND mi.location_id=ii.location_id 
SET mi.inventory_item_id = ii.id 
+0

對不起,我的問題措辭可能很差。我試圖刪除這些硬編碼的值。將mi.bin_number = ii.bin_number和mi.location = ii.location做最後的where語句? – timpone 2014-09-20 03:15:58

+0

酷,thx - 看起來像它應該:)必須等待幾分鐘 – timpone 2014-09-20 03:17:50