指定值为71时排除更新特定列的代码异常问题排查
问题分析与解决方案
排除列逻辑自相矛盾:
你把list_type放进了标注为「要排除更新」的$columns数组,但后续循环里并没有跳过这个列,反而对它生成了tmp.list_type = fast.list_type的更新语句——这等于完全没实现“排除”的效果,list_type会被无条件同步fast表的值。CASE语句逻辑写反:
你后续添加的CASE语句是「当fast.list_type为71时,把tmp.list_type设为71;否则保持原值」,这和你「71时排除更新」的需求完全相反,反而会在值为71时强制更新。
针对你的需求(当值为71时排除更新,其他值正常更新)的正确代码:
// 要排除更新的列(移除list_type,我们单独处理它的逻辑) $columns = array('`fast`', '`foo`'); $columnsForUpdate = array(); foreach($columns as $column) { if($column != '`vid`' && in_array($column, $columnsCommon)) { $columnsForUpdate[] = "`tmp`.{$column} = `fast`.{$column}"; } } // 单独处理list_type:仅当fast.list_type不是71时才更新,71时保持原值 if (in_array('`list_type`', $columnsCommon)) { $columnsForUpdate[] = "`tmp`.`list_type` = CASE WHEN `fast`.`list_type` != '71' THEN `fast`.`list_type` ELSE `tmp`.`list_type` END"; }
如果你实际需求是「无论值是什么,都不更新list_type」:
// 要排除更新的列,包含list_type $columns = array('`fast`', '`foo`', '`list_type`'); $columnsForUpdate = array(); foreach($columns as $column) { // 跳过list_type,只处理其他需要更新的列 if($column != '`vid`' && $column != '`list_type`' && in_array($column, $columnsCommon)) { $columnsForUpdate[] = "`tmp`.{$column} = `fast`.{$column}"; } } // 不要添加任何list_type相关的更新语句
内容的提问来源于stack exchange,提问作者user8411456
相关产品推荐
相关产品推荐

