sql update from 修改一个表的值来自另一个表

时间:2023-03-09 09:44:14
sql update from 修改一个表的值来自另一个表

假设有桌子表名 icate_table_set(table_id,table_name,table_state_id,store_id), 桌子状态表名icate_table_state(state_id,state_name,state_attr状态属性如空桌,使用中等,store_id)

store_id假设是分店ID  各分店的状态名可能一样可能不一样但是属性是一样的

现在要把所有桌子改为对应如空桌状态(1为空桌)

UPDATE icate_table_set

SET table_state_id=(SELECT state_id FROM icate_table_state WHERE state_attr='1' AND store_id=a.store_id)

from icate_table_set a,icate_table_state b