SQL237. 将所有to_date为9999-01-01的全部更新为NULL
描述
将所有to_date为9999-01-01的全部更新为NULL,且 from_date更新为2001-01-01。CREATE TABLE IF NOT EXISTS 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');
id | emp_no | title | from_date | to_date |
1 | 10001 | Senior Engineer | 2001-01-01 | NULL |
2 | 10002 | Staff | 2001-01-01 | NULL |
3 | 10003 | Senior Engineer | 2001-01-01 | NULL |
4 | 10004 | Senior Engineer | 2001-01-01 | NULL |
5 | 10001 | Senior Engineer | 2001-01-01 | NULL |
6 | 10002 | Staff | 2001-01-01 | NULL |
7 | 10003 | Senior Engineer | 2001-01-01 | NULL |
select count(*) from titles_test where from_date='2001-01-01' and to_date is NULL;
示例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');
输出:
7
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3256KB, 提交时间: 2021-12-31
update titles_test set to_date =null , from_date ='2001-01-01' where to_date='9999-01-01'
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3328KB, 提交时间: 2021-08-04
update titles_test set to_date = null , from_date = '2001-01-01' where to_date = '9999-01-01'
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3328KB, 提交时间: 2021-06-15
update titles_test set to_date = NULL,from_date = "2001-01-01" where to_date = "9999-01-01";
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3328KB, 提交时间: 2021-05-28
UPDATE titles_test SET to_date=NULL ,from_date='2001-01-01' WHERE to_date='9999-01-01';
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3340KB, 提交时间: 2021-07-22
Update titles_test set to_date = NULL, from_date='2001-01-01' where to_date='9999-01-01'