簡體   English   中英

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

[英]MySQL - Delete value in the row, instead of deleting the row

如果一行包含搜索值,我想刪除搜索值(而不是行)。 例如,如果我要刪除香蕉,則應僅從包含香蕉的行中刪除香蕉。

試過這個

DELETE FROM users where eat like '%banana%' 

但是,它將刪除行。 那么如何從行中僅刪除搜索值?

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

要么

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

你可以試試這個-

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

這將僅替換存在區域中的eat欄中的banana

更新資料

遍歷數據並替換這些值。 這可能有幫助-

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

試試這個查詢:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM