官网链接:
更新记录(二)_牛客题霸_牛客网现有一张试卷作答记录表exam_record,其中包含多年来的用户作答试卷记录,结构如下表。题目来自【牛客题霸】https://www.nowcoder.com/practice/0c2e81c6b62e4a0f848fa7693291defc?tpId=240&tqId=2223560&ru=%2Fpractice%2Ff6b4770f453d4163acc419e3d19e6746&qru=%2Fta%2Fsql-advanced%2Fquestion-ranking&sourceUrl=
0 问题描述
现有一张试卷作答记录表exam_record,需求:将 exam_record表中2021年9月1日之前开始作答的未完成记录全部改为被动完成,即:将完成时间改为'2099-01-01 00:00:00',分数改为0。
1 数据准备
CREATE TABLE IF NOT EXISTS exam_record (
id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
uid int NOT NULL COMMENT '用户ID',
exam_id int NOT NULL COMMENT '试卷ID',
start_time datetime NOT NULL COMMENT '开始时间',
submit_time datetime COMMENT '提交时间',
score tinyint COMMENT '得分'
)CHARACTER SET utf8 COLLATE utf8_general_ci;
INSERT INTO exam_record(uid,exam_id,start_time,submit_time,score) VALUES
(1001, 9001, '2020-01-02 09:01:01', '2020-01-02 09:21:01', 80),
(1001, 9002, '2021-09-01 09:01:01', '2021-09-01 09:21:01', 90),
(1002, 9001, '2021-08-02 19:01:01', null, null),
(1002, 9002, '2021-09-05 19:01:01', '2021-09-05 19:40:01', 89),
(1003, 9001, '2021-09-02 12:01:01', null, null),
(1003, 9002, '2021-09-01 12:01:01', null, null);
2 数据分析
update exam_record
set submit_time ='2099-01-01 00:00:00' ,score =0
where start_time < '2021-09-01 00:00:00' and score is null;
3 小结
mysql中修改记录的方式:
- 设置为新值:
update table_name set column_name = new_value [, column_name2 = new_value2 ] [where column_name3 = value3 ]
- 根据已有值替换:
update table_name set key1 = replace(key1,'查找内容','替换成内容') [where column_name3 = value3 ]
MySQL 更新数据 不同条件(批量)更新不同值_update批量更新同一列不同值-CSDN博客