SQL113. 更新记录(一)
描述
现有一张试卷信息表examination_info,表结构如下图所示:
Filed | Type | Null | Key | Extra | Default | Comment |
id | int(11) | NO | PRI | auto_increment | (NULL) | 自增ID |
exam_id | int(11) | NO | UNI | | (NULL) | 试卷ID |
tag | char(32) | YES | | | (NULL) | 类别标签 |
difficulty | char(8) | YES | | | (NULL) | 难度 |
duration | int(11) | NO | | | (NULL) | 时长 |
release_time | datetime | YES | | | (NULL) | 发布时间 |
示例1
输入:
drop table if EXISTS examination_info; CREATE TABLE IF NOT EXISTS examination_info ( id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID', exam_id int UNIQUE NOT NULL COMMENT '试卷ID', tag varchar(32) COMMENT '类别标签', difficulty varchar(8) COMMENT '难度', duration int NOT NULL COMMENT '时长', release_time datetime COMMENT '发布时间' )CHARACTER SET utf8 COLLATE utf8_bin; TRUNCATE examination_info; INSERT INTO examination_info(exam_id,tag,difficulty,duration,release_time) VALUES (9001, 'SQL', 'hard', 60, '2020-01-01 10:00:00'), (9002, 'python', 'easy', 60, '2020-01-01 10:00:00'), (9003, 'Python', 'medium', 80, '2020-01-01 10:00:00'), (9004, 'PYTHON', 'hard', 80, '2020-01-01 10:00:00');
输出:
9001|SQL|hard|60|2020-01-01 10:00:00 9002|python|easy|60|2020-01-01 10:00:00 9003|Python|medium|80|2020-01-01 10:00:00 9004|Python|hard|80|2020-01-01 10:00:00
Mysql 解法, 执行用时: 36ms, 内存消耗: 6184KB, 提交时间: 2021-12-13
UPDATE examination_info set tag ='Python' WHERE tag='PYTHON'
Mysql 解法, 执行用时: 36ms, 内存消耗: 6188KB, 提交时间: 2022-01-24
UPDATE examination_info SET tag='Python' WHERE tag='PYTHON'
Mysql 解法, 执行用时: 36ms, 内存消耗: 6192KB, 提交时间: 2022-01-25
##第一种 update examination_info set tag=replace(tag,'PYTHON','Python') where tag='PYTHON'
Mysql 解法, 执行用时: 36ms, 内存消耗: 6212KB, 提交时间: 2022-01-24
UPDATE examination_info SET tag='Python' WHERE tag='PYTHON'
Mysql 解法, 执行用时: 36ms, 内存消耗: 6224KB, 提交时间: 2022-01-27
update examination_info set tag = "Python" where tag = 'PYTHON';