mysql條件判斷並批量修改資料

2021-09-28 10:36:12 字數 555 閱讀 4653

要批量修改欄位中特定值的資料時,有兩種方法可以修改

方法一:

update table_name set column_name=replace(column_name, 'none', null)

注釋:修改表(table_name)中列(column_name)的值,當值等於字串'none'時改為null

這裡只修改一列時不會有問題,或者修改多列的型別是一樣時也不會有問題,也就是當修改的列有交叉時會統一修改所有列的值

方法二:

update table_name set column_name_1=case when column_name_1='none' then null else column_name_1 end,column_name_2=case when column_name_2='none' then null else column_name_2 end

注釋:修改表(table_name)中列(column_name_1,column_name_2)的值,當資料等於字串'none'時將值修改為null,否則不變,修改多列且資料不一致時方法二比方法一靠譜

mysql量批修改 MySQL的批量修改資料

使用自帶的語句構建批量更新 mysql 實現批量 可以用點小技巧來實現 update tablename set orderid case id when 1 then 3 when 2 then 4 when 3 then 5 endwhere id in 1,2,3 這句sql 的意思是,更新o...

游標 批量修改資料

批量修改表中資料 declare my cursor cursor 定義游標 for select from table where id 查出需要的集合放到游標中 open my cursor 開啟游標 fetch next from my cursor 讀取第一行資料 while fetch s...

SQL批量修改資料

1 批量替換 期中的年份 以替換為2019年為例 update 資料表名 set 期時間字段 stuff convert nvarchar 23 期時間字段,120 1,4,2019 2 批量替換 期中的 份 以替換為6 為例 update 資料表名 set 期時間字段 stuff convert ...