(1)根据条件更新值根据指定条件更新(多列)(全部更新)
update 表名
set 列名1 = 值1 [,列名2=值2]
[where 条件];
替换指定值(多列)(部分更新)
update 表名
set 列名1 = replace(列名1, '查找内容', '替换内容') [,列名2 = replace(列名2, '查找内容', '替换内容')]
[where 条件];
update table
set
列名1 = if(条件1,值1,值2),
列名2 = if(条件2,值3,值4)
[where 条件];
使用 case when
update table
set 列名1 =
end
[where 条件];
students 表 (id表示主键,name是姓名,score是平均成绩)
(1)根据条件更新值把 students 表中 name 为张三的 score 字段的值全部修改为100.
#使用where
update students
set score = 100
where name = '张三';
#使用replace
update students
set score = replace(score,59,0),
name = replace(name,'三','四')
where id >= 2;
注意:张三替换之后是张四,并不是只有字段等于三时才能替换.
#批量更新多值 + if
update students
set
score = if(score < 60,0,100),
name = if(score < 60,'不及格','及格');
注意:更新的值要满足建表时的字段类型.比如score是int类型就不能更新为char类型.
#批量更新多值 + case when
update students
set
when score < 60 then '不及格'
when score >= 90 then '优秀'
when score < 60 then 0
when score >= 90 then 2
end;
注意:更新的时候是按照代码语句的先后顺序更新的.可以尝试先更新score后更新name,结果是不一样的.
以上就是土嘎嘎小编为大家整理的MySQL 如何实现数据更新相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!