MySQL - 删除行中的值,而不是删除行

时间:2016-02-08 08:00:06

标签: php mysql

如果某行包含搜索到的值,我想删除搜索到的值(不是行)。例如,如果我想删除banana,它应该只从包含banana的行中删除banana。

试过这个,

DELETE FROM users where eat like '%banana%' 

但是,它删除了行。那么如何才能从行中删除搜索值呢?

4 个答案:

答案 0 :(得分:2)

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

OR

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

答案 1 :(得分:2)

你可以试试这个 -

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

这只会替换banana列中存在的eat

<强>更新

循环遍历数据并替换这些值。这可能会有所帮助 -

$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 . "%'"
}

答案 2 :(得分:1)

尝试此查询:

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

答案 3 :(得分:1)

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