2014-09-20 79 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