SQL238. 将id=5以及emp_no=10001的行数据替换成id=5以及emp_no=10005
描述
将id=5以及emp_no=10001的行数据替换成id=5以及emp_no=10005,其他数据保持不变,使用replace实现,直接使用update会报错。CREATE TABLE titles_test ( id int(11) not null primary key, emp_no int(11) NOT NULL, title varchar(50) NOT NULL, from_date date NOT NULL, to_date date DEFAULT NULL); insert into titles_test values ('1', '10001', 'Senior Engineer', '1986-06-26', '9999-01-01'), ('2', '10002', 'Staff', '1996-08-03', '9999-01-01'), ('3', '10003', 'Senior Engineer', '1995-12-03', '9999-01-01'), ('4', '10004', 'Senior Engineer', '1995-12-03', '9999-01-01'), ('5', '10001', 'Senior Engineer', '1986-06-26', '9999-01-01'), ('6', '10002', 'Staff', '1996-08-03', '9999-01-01'), ('7', '10003', 'Senior Engineer', '1995-12-03', '9999-01-01');
示例1
输入:
drop table if exists titles_test; CREATE TABLE titles_test ( id int(11) not null primary key, emp_no int(11) NOT NULL, title varchar(50) NOT NULL, from_date date NOT NULL, to_date date DEFAULT NULL); insert into titles_test values ('1', '10001', 'Senior Engineer', '1986-06-26', '9999-01-01'), ('2', '10002', 'Staff', '1996-08-03', '9999-01-01'), ('3', '10003', 'Senior Engineer', '1995-12-03', '9999-01-01'), ('4', '10004', 'Senior Engineer', '1995-12-03', '9999-01-01'), ('5', '10001', 'Senior Engineer', '1986-06-26', '9999-01-01'), ('6', '10002', 'Staff', '1996-08-03', '9999-01-01'), ('7', '10003', 'Senior Engineer', '1995-12-03', '9999-01-01');
输出:
5|10005|Senior Engineer|1986-06-26|9999-01-01
Sqlite 解法, 执行用时: 9ms, 内存消耗: 3308KB, 提交时间: 2021-05-31
update titles_test set emp_no = REPLACE(emp_no, 10001, 10005) WHERE id = 5;
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3176KB, 提交时间: 2020-12-25
update titles_test set emp_no=replace(emp_no,'10001','10005') where id=5
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3180KB, 提交时间: 2021-06-22
update titles_test set emp_no=replace(emp_no,10001,10005) where id=5;
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3192KB, 提交时间: 2020-12-23
update titles_test set emp_no=replace(emp_no,'10001','10005') where id = 5 and emp_no='10001'
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3192KB, 提交时间: 2020-11-23
update titles_test set emp_no=replace(emp_no,'10001','10005') where id=5 and emp_no='10001'