简体   繁体   中英

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?

Should it be done at the code of PHP or MySQL?

the flow is:

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;

Because book3, book4 and book5 will be updated to the value of N , so I skip this fields

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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