MySQL - Delete value in the row, instead of deleti

2019-08-29 06:18发布

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 ?

标签: php mysql
4条回答
你好瞎i
2楼-- · 2019-08-29 06:19
 Update users  
 set eat =  'your value' 
 where eat like '%banana%' ;
查看更多
老娘就宠你
3楼-- · 2019-08-29 06:23
UPDATE users SET eat = null where eat like '%banana%';

OR

UPDATE users SET eat = '' where eat like '%banana%';
查看更多
Fickle 薄情
4楼-- · 2019-08-29 06:36

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 . "%'"
}
查看更多
Animai°情兽
5楼-- · 2019-08-29 06:40

Try this query :

update users set eat='' where eat like '%banana%' 
查看更多
登录 后发表回答