设备表ops_device_info中的终端号terminal_id值是以 'D'开头的字符串,而终端表ops__terminal_info中的终端号terminal_id是8位字符串, 它们之间是通过device_id关联的.
ops_device_info 1-->n ops_terminal_info
现在希望将设备表中的终端号更改为终端表中的终端号.
终端状态 4代表移机 5代表调试 6代表暂停 7代表启用 8代表报废 9代表停用
方式一:
update ops_device_info d set d.terminal_id = (select t.terminal_id from ops_terminal_info t where t.device_id=d.device_id and d.terminal_id like 'D%' and t.terminal_state not in ('8','9')) where exists (select t.terminal_id from ops_terminal_info t where t.device_id=d.device_id and d.terminal_id like 'D%' and t.terminal_state not in ('8','9'));
方式二:
merge into OPS_DEVICE_INFO t1using OPS_TERMINAL_INFO t2on (t1.device_id = t2.DEVICE_ID and t2.TERMINAL_STATE not in ('8','9'))when matched then Update set t1.TERMINAL_ID = t2.TERMINAL_ID where t1.terminal_id like 'D%';
SQl执行过程中如果出现 '单行子查询返回多行记录',那可能是你的数据本身有点重复。; 可以使用 我的另一个sql查询一下重复数据 .