简体   繁体   中英

replace substring with and without comma

cnt is a text column like this:

lorem,ipsum,sky,dolor
lorem,sky,ipsum
lorem,ipsum,dolor,sky

I want to remove sky from entire column

$sq = "update table set cnt = replace(cnt, :a, :b)";
    $st = $db->prepare($sq);
    $st->execute([
        ":a" => 'sky'
        ":b" => ''
    ]);

Problem
sky sometimes has a commma - it should be removed also
and sometimes is without comma (at the end of string)

How to remove sky with and without comma?

UPDATE test
SET cnt = TRIM( BOTH ',' 
                FROM REPLACE(REPLACE(CONCAT(',', cnt, ','), 
                                     CONCAT(',', @a, ','), 
                                     CONCAT(',', @b, ',')),
                             ',,',
                             ','));

fiddle

$sq = "update table set cnt = replace(cnt, :a, '')";
$st = $db->prepare($sq);
$st->execute([":a" => $a . ',']);
$st->execute([":a" => $a]);

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