首页 > 试题广场 >

将所有to_date为9999-01-01的全部更新为NUL

[编程题]将所有to_date为9999-01-01的全部更新为NUL
  • 热度指数:103268 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
将所有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');
更新后的值:
titles_test 表的值:
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
后台会执行下面SQL语句得到输出,判断正确:
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
update
    titles_test
set
    to_date = null,
    from_date = '2001-01-01'
where
    to_date = '9999-01-01';

发表于 2025-01-06 14:41:17 回复(0)
UPDATE table_name
SET column1 = value1, column2 = value2, ... WHERE condition;

参数说明:

  • table_name 是你要更新数据的表的名称。
  • column1, column2, ... 是你要更新的列的名称。
  • value1, value2, ... 是新的值,用于替换旧的值。
  • WHERE condition 是一个可选的子句,用于指定更新的行。如果省略 WHERE 子句,将更新表中的所有行。
发表于 2024-10-30 18:19:09 回复(0)
update titles_test
set to_date = replace(to_date, "9999-01-01", null),
    from_date = "2001-01-01";

发表于 2024-06-12 21:16:28 回复(0)
update titles_test set to_date=null,from_date='2001-01-01';

编辑于 2024-02-05 13:33:42 回复(0)
update titles_test 
set to_date = null, from_date = '2001-01-01'
where to_date = '9999-01-01';
发表于 2024-01-22 22:11:05 回复(0)
update titles_test
set to_date=null,from_date="2001-01-01"
where to_date="9999-01-01"

发表于 2023-03-17 18:42:26 回复(0)
update titles_test set to_date = NULL ,from_date = '2001-01-01'
where to_date = '9999-01-01';

发表于 2022-12-27 23:42:35 回复(0)
注意若干列 to_date = NULL 和 from_date = '2001-01-01' 之间只能用逗号连接,切勿用
AND 连接。
1
2
UPDATE titles_test SET to_date = NULL, from_date = '2001-01-01'
WHERE to_date = '9999-01-01';
发表于 2022-07-08 09:22:43 回复(0)
update titles_test 
set to_date = null, from_date = "2001-01-01"
where to_date = "9999-01-01";
发表于 2022-06-02 16:04:56 回复(0)
UPDATE titles_test 
SET TO_DATE = NULL,FROM_DATE = '2001-01-01'
WHERE TO_DATE = '9999-01-01'
;
发表于 2022-04-16 16:54:57 回复(0)
update titles_test set from_date='2001-01-01',to_date=null where to_date='9999-01-01'

发表于 2022-03-20 09:39:32 回复(0)
update titles_test
set to_date =null
, from_date ='2001-01-01'
where to_date='9999-01-01'


发表于 2022-03-19 18:25:26 回复(0)
update titles_test
set to_date = null
where to_date = "9999-01-01";
update titles_test
set from_date = "2001-01-01";

发表于 2022-03-12 13:28:39 回复(0)
UPDATE titles_test
SET to_date = NULL,
    from_date = '2001-01-01'
WHERE to_date = '9999-01-01';
发表于 2022-01-28 12:04:49 回复(0)
UPDATE titles_test
SET to_date = NULL, 
    from_date = '2001-01-01'
WHERE to_date ='9999-01-01'; 
记得用逗号「,」分隔SET后面的内容
发表于 2022-01-25 21:39:06 回复(0)
1. UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值;
2. NULL 不带引号,时间带引号;
3.更新多列时每列用逗号分隔。
发表于 2021-12-06 10:07:53 回复(0)
UPDATE
titles_test
SET
to_date=REPLACE(to_date,'9999-01-01',null),FROM_date='2001,01,01';
这是什么原理,set后的语句是同时改变的吗

发表于 2021-11-25 20:41:15 回复(0)