我可以检查更新值是否为N
,然后跳过此字段吗?
应该在PHP或MySQL的代码中完成吗?
流程是:
BEFORE
update store set book1 = 'Y', book2 = 'Y', book3 = 'N', book4 = 'N', book5 = 'N' where id =
1;
AFTER
update store set book1 = 'Y', book2 = 'Y' where id =
1;
因为book3,book4和book5将更新为N
的值,所以我跳过这些字段
答案 0 :(得分:1)
这是一个非常开放的问题。这取决于您如何为脚本构建SQL代码,但如果您想更改SQL,那么在PHP中执行它将是您最好的选择。
作为一个简单的例子,你可以这样做:
$books = array ( 'book1' => 'Y',
'book2' => 'Y',
'book3' => 'N',
'book4' => 'N',
);
$updates = array();
foreach ($books as $book => $value) {
if ($value != 'N') {
$updates[] = "$book = '$value'";
}
}
$update = implode(', ', $updates);
$query = 'update store set '.$update.' where id =1;';