首页 > 试题广场 >

更新记录(二)

[编程题]更新记录(二)
  • 热度指数:81575 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
现有一张试卷作答记录表exam_record,其中包含多年来的用户作答试卷记录,结构如下表:
作答记录表exam_record:
submit_time为 完成时间
Filed Type Null Key Extra Default Comment
id int(11) NO PRI auto_increment (NULL) 自增ID
uid int(11)
NO


(NULL)
用户ID
exam_id int(11) NO


(NULL)
试卷ID
start_time datetime NO


(NULL)
开始时间
submit_time datetime YES

(NULL)
提交时间
score tinyint(4)
YES


(NULL)
得分

请把exam_record表中2021年9月1日之前开始作答的未完成记录全部改为被动完成,即:将完成时间改为'2099-01-01 00:00:00',分数改为0。
示例1

输入

drop table if EXISTS exam_record;
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);

输出

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|2099-01-01 00:00:00|0
1002|9002|2021-09-05 19:01:01|2021-09-05 19:40:01|89
1003|9001|2021-09-02 12:01:01|None|None
1003|9002|2021-09-01 12:01:01|None|None
update exam_record
set submit_time='2099-01-01 00:00:00',score=0
where start_time<'2021-09-01' and submit_time is null
#注意set后面有多个对象用','而不是'and'连接

发表于 2021-12-05 17:13:16 回复(4)
update exam_record set submit_time = '2099-01-01 00:00:00',score = 0
WHERE date(start_time)<date('2021-9-1')
and submit_time is NULL

Mysql面试还是经常问啊
发表于 2021-11-20 14:13:19 回复(0)
update exam_record
set submit_time='2099-01-01 00:00:00',score=0
where start_time <'2021-09-01' and submit_time is null;
中2021年9月1日之前开始作答的未完成记录:
where start_time <'2021-09-01' and submit_time is null
update… set…where…更新数据
发表于 2021-10-26 11:15:06 回复(0)

【场景】:按照条件(批量)更新多值

【分类】:更新记录、update + if、update + case

分析思路

难点:

1.根据条件更新多值。值之间加逗号

方法一、根据指定条件更新

  • [使用]:利用where条件查找行后,对列名1字段中的值更新为值1
update 表名 set 列名1 = 值1 [,列名2=值2] [where 条件];

方法二、批量更新

  • 使用 if 不同条件更新不同内容
update 表名 
set 
    列名1 = if(条件1,值1,值2),
    列名2 = if(条件2,值3,值4)
[where 条件];
  • 使用 case when 不同条件更新不同内容
update 表名 
set 列名1 =
    case
        when 条件1 then 值1
        when 条件2 then 值2
        ...
    end,
    列名2 =
    case
        when 条件21 then 值21
        when 条件22 then 值22
        ...
    end
[where 条件];

扩展

前往查看:MySQL 更新数据

求解代码

方法一:

update + where

update exam_record 
set 
    submit_time = '2099-01-01 00:00:00',
    score = 0
where date(start_time) < date('2021-09-01') and score is null

方法二:

使用批量更新多值 + if

update exam_record 
set 
    submit_time = if(date(start_time) < date('2021-09-01') and score is null,'2099-01-01 00:00:00',submit_time),
    score = if(date(start_time) < date('2021-09-01') and score is null,0,score)

方法三:

使用批量更新多值 + case when

update exam_record 
set 
    submit_time = case
        when date(start_time) < date('2021-09-01') and score is null then '2099-01-01 00:00:00'
        else submit_time
    end,
    score = case
        when date(start_time) < date('2021-09-01') and score is null then 0
        else score
    end
发表于 2022-12-17 21:12:13 回复(1)
题目:请把exam_record表中2021年9月1日之前开始作答的未完成记录全部改为被动完成,即:将完成时间改为'2099-01-01 00:00:00',分数改为0。
感觉像是在做阅读理解,哈哈哈!!!
条件:2021年9月1日之前开始作答
            未完成记录
update exam_record
set submit_time = '2099-01-01 00:00:00', score = 0
where start_time < '2021-09-01' and submit_time is null

发表于 2022-03-03 11:48:39 回复(0)
UPDATE exam_record
SET submit_time = '2099-01-01 00:00:00',
    score = 0
WHERE start_time < '2021-9-1' AND score IS NULL
发表于 2021-10-24 21:11:04 回复(0)
update exam_record
set score = 0, submit_time = "2099-01-01 00:00:00"
where submit_time is null
and "2021-09-01" > date_format(start_time, "%Y-%m-%d");
发表于 2022-04-12 19:40:34 回复(0)
update exam_record set submit_time='2099-01-01 00:00:00',score=0
where start_time<'2021-09-01' and submit_time is null

发表于 2021-11-09 11:27:15 回复(0)
The question's wording is quite confusing. Both of the following solutions can pass the test:
1.
update exam_record
set submit_time = '2099-01-01 00:00:00', score = 0
where start_time<'2021-09-01' and score is null

2.
update exam_record
set submit_time = '2099-01-01 00:00:00', score = 0
where start_time<'2021-09-01' and submit_time is null

发表于 2022-08-19 07:03:07 回复(0)
update exam_record
set submit_time='2099-01-01 00:00:00',
score='0'
where start_time<'2021-09-01' and submit_time is null 
-- 注意“2021年9月1日之前开始作答的未完成记录”需要限定两个条件
发表于 2022-03-06 10:43:25 回复(0)
update exam_record
set submit_time=ifnull(submit_time,'2099-01-01 00:00:00'),
score=ifnull(score,0)
where start_time<'2021-09-01'

发表于 2025-03-17 16:00:34 回复(0)
UPDATE
    exam_record
SET
    submit_time = '2099-01-01 00:00:00',
    score = 0
WHERE
    submit_time IS NULL
    AND
    start_time < '2021-09-01';

发表于 2025-01-07 21:19:38 回复(0)
不能= 要用is null
发表于 2024-12-16 09:53:38 回复(0)
UPDATE exam_record
SET submit_time = '2099-01-01 00:00:00',
    score = 0
WHERE DATE(start_time) < '2021-09-01 00:00:00'
    AND submit_time IS NULL;

发表于 2024-11-21 17:22:15 回复(0)
is Null
发表于 2024-10-06 18:52:03 回复(0)
update exam_record
set 
    submit_time = '2099-01-01 00:00:00',
    score = 0
where 
    date(start_time) < date('2021-09-01') and submit_time is null;

在比较日期,没有时间的时候,最好还是使用date函数,防止出现逾期之外的错误
发表于 2024-08-12 09:47:02 回复(0)
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;
发表于 2024-07-07 11:48:54 回复(0)
update exam_record set submit_time=date('2099-01-01'),score='0' where date(start_time) < date('2021-09-01') and submit_time is null and score is null


发表于 2024-04-17 12:35:58 回复(0)
update exam_record
set submit_time = '2099-01-01 00:00:00',score = 0
where date(start_time) < '2021-09-01 00:00:00' 
and submit_time is null

发表于 2024-04-11 15:41:36 回复(0)
没怎么理解题目意思
update exam_record set submit_time=20990101000000,score=0
where start_time<"2021-09-01" and submit_time is null;


发表于 2024-03-27 17:24:59 回复(0)