我正在使用MySQL 5.5,我有两个表,inventory_items和menu_items。它们都有一个bin_number,它是一个字符串,location_id是一个整数。
menu_items
invetory_item_id | location_id | bin_number
inventory_items
id | location_id | bin_number
我想将menu_items inventory_item_id更新为inventory_items表的id,它们在bin_number和location_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更新到menu_items和inventory_items之间bin_number和location_id相同的位置?
THX
答案 0 :(得分:1)
您可以在JOIN
中使用UPDATE
:
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