首页 > 试题广场 >

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

[编程题]将所有to_date为9999-01-01的全部更新为NUL
  • 热度指数:101729 时间限制: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'
;
发表于 2022-04-16 16:54:57 回复(0)
update `titles_test`
set to_date = null,
from_date = '2001-01-01'
where to_date = '9999-01-01';

发表于 2021-08-15 22:52:35 回复(0)
update  titles_test
set to_date = null,from_date = '2001-01-01'
where to_date = '9999-01-01';
发表于 2021-05-07 15:42:10 回复(0)
考点:update 更新语句的基本使用
update titles_test 
set from_date = '2001-01-01', to_date = null
where to_date = '9999-01-01';


发表于 2021-03-27 14:48:00 回复(0)
语言运行环境:Sql(mysql 8.0)
更新记录的语句为 update 表名 set列名=列值 where限制条件
不要忘记set
update titles_test 
set to_date=null, from_date='2001-01-01'
where to_date='9999-01-01';


发表于 2021-01-25 13:13:44 回复(0)
update titles_test 
set to_date = NULL,
from_date = '2001-01-01'
where to_date = '9999-01-01'

发表于 2020-08-09 18:12:22 回复(0)
update titles_test
set to_date=NULL, from_date='2001-01-01'
where to_date='9999-01-01'
;
发表于 2020-07-17 16:43:46 回复(0)
用 UPDATE 语句更新若干列的最基本用法,详细可参考:
另外要注意若干列 to_date = NULL 和 from_date = '2001-01-01' 之间只能用逗号连接,切勿用 AND 连接。
UPDATE titles_test SET to_date = NULL, from_date = '2001-01-01' 
WHERE to_date = '9999-01-01';

编辑于 2017-07-22 15:39:17 回复(22)
update titles_test set to_date=NULL, from_date='2001-01-01' where to_date='9999-01-01'
需要注意的是set后面的内容用逗号隔开,而不是and
发表于 2017-12-13 17:23:33 回复(0)
update  titles_test set to_date=null ,from_date ='2001-01-01' 
where to_date='9999-01-01'
set 字段名1=字段值1,字段名2=字段值2  ,用逗号","分隔,不是用空格分隔
编辑于 2017-08-30 23:53:28 回复(0)

sqlLite中带有 WHERE 子句的 UPDATE 查询的基本语法如下(可看出和MySql的一样):

UPDATE table_name
SET column1 = value1,
   column2 = value2....,
   columnN = valueN
WHERE [condition];

您可以使用 AND 或 OR 运算符来结合 N 个数量的条件

所以,本题的结果表示为:

update titles_test 
set to_date = null,
    from_date = '2001-01-01' 
where to_date = '9999-01-01'
编辑于 2018-09-06 17:49:28 回复(0)
update titles_test
set to_date = NULL,
from_date = '2001-01-01'
where to_date = '9999-01-01';
-- 上述语句有时无法在MySQL workbench中运行,是因为MySql运行在safe-updates模式下,该模式会导致非主键条件下无法执行update或者delete命令,执行命令SET SQL_SAFE_UPDATES = 0;修改下数据库模式
只需要执行SET SQL_SAFE_UPDATES = 0;
然后再执行上述语句即可
-- 如果想要提高数据库安全等级,可以在恢复回原有的设置,执行命令:SET SQL_SAFE_UPDATES = 1;

执行成功后,以delete命令为例,非主键情况下又报错了,说明安全等级修改成功

编辑于 2018-05-01 19:13:51 回复(0)
update titles_test 
set to_date=null,from_date='2001-01-01'
where to_date='9999-01-01'

发表于 2023-09-06 15:29:41 回复(0)
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=NULL,from_date='2001-01-01'
where to_date='9999-01-01'

发表于 2024-10-30 14:32:08 回复(0)
UPDATE titles_test
SET from_date = '2001-01-01', to_date = NULL
WHERE to_date = '9999-01-01'
发表于 2024-10-24 16:53:27 回复(0)
update titles_test
set from_date = "2001-01-01",to_date = null
where to_date = "9999-01-01"
update table
set col='',col2='',col3=''
where id = ''
发表于 2024-10-06 21:52:33 回复(0)
update `titles_test`
set `to_date`=null,`from_date`='2001-01-01'
where `to_date`='9999-01-01';

发表于 2024-07-18 10:40:29 回复(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)