简体   繁体   English

如果更新值是特定字符串,则跳过以更新表字段

[英]Skip to update the table field if the update value is the specific string

Can I check if the update value is N , then skip this field? 我可以检查更新值是否为N ,然后跳过此字段吗?

Should it be done at the code of PHP or MySQL? 应该用PHP还是MySQL的代码完成?

the flow is: 流程是:

BEFORE 之前

update store set book1 = 'Y', book2 = 'Y', book3 = 'N', book4 = 'N', book5 = 'N' where id = 1; 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; update store set book1 = 'Y', book2 = 'Y' where id = 1;

Because book3, book4 and book5 will be updated to the value of N , so I skip this fields 因为book3,book4和book5将更新为N的值,所以我跳过了此字段

This is a very open ended question. 这是一个非常开放的问题。 It depends how you are building the SQL code for your script, but if you want to change the SQL then doing it in PHP will be your best bet. 这取决于您如何为脚本构建SQL代码,但是如果要更改SQL,那么最好在PHP中进行。

As a quick example, you could do something like this: 作为一个简单的例子,您可以执行以下操作:

$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;';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM