user198989 February 2016

MySQL - Delete value in the row, instead of deleting the row

I want to delete the searched value (not row) if a row contains the searched value. For example, if I want to remove banana, it should only remove banana from the rows which contain banana.

Tried this,

DELETE FROM users where eat like '%banana%' 

However, it removes the rows. So how can I remove only search value from the rows ?

Answers


SohelAhmedM February 2016

UPDATE users SET eat = null where eat like '%banana%';

OR

UPDATE users SET eat = '' where eat like '%banana%';


Mr. Engineer February 2016

Try this query :

update users set eat='' where eat like '%banana%' 


scaisEdge February 2016

 Update users  
 set eat =  'your value' 
 where eat like '%banana%' ;


Sougata February 2016

You can try this -

UPDATE users SET eat = REPLACE(eat, 'banana', '') where eat like '%banana%';

This would replace only banana from eat column where it is present.

Update

Loop through the data and replace those values. This might help -

$check_val = 'banana';

//select those rows first
"select id, eat from users where eat like '%" . $check_val . "%'"

foreach($data as $v) {

    $temp= explode(',', $v['eat']);
    $temp= array_map(function($t) use($check_val) {
        return (strpos($t, $check_val) !== false) ? null : $t;
    }, $temp);
    $temp = array_filter($temp);
    $v['eat']= implode(',', $temp);

    "update users set eat= '" . $v['eat'] . "' where eat like '%" . $check_val . "%'"
}

Post Status

Asked in February 2016
Viewed 3,165 times
Voted 10
Answered 4 times

Search




Leave an answer