oracle数据库中修改表中某字段的值等于另一个表中字段的值

2024-11-03 12:27:16
推荐回答(3个)
回答1:

1、创建测试表,

create table test_t1(id varchar(30) , EventTime date);

create table test_t2(id varchar(30) , C_date date);

2、插入测试数据

insert into test_t1 values(1,sysdate-1);

insert into test_t1 values(2,sysdate-2);

insert into test_t1 values(3,sysdate-3);

insert into test_t2 values(1,null);

insert into test_t2 values(1,null);

insert into test_t2 values(1,null);

commit;

3、查询T2表中数据,可以发现c_date字段全部为空,select t.*, rowid from test_t2 t;

4、编写sql,修改T2表中的C_date字段的值,等于T1表中的EventTime的值;update test_t2 t2 set t2.c_date = (select eventtime from test_t1 t1 where t1.id = t2.id)

5、再次查询T2表中数据,可以发现c_date字段全部为T1表中对应的数据;select t.*, rowid from test_t2 t;

回答2:

UPDATE t2 SET t2.C_date = (select EventTime from t1 where t1.id = t2.id)

直接用

回答3:

UPDATE t2 SET t2.C_date = (select EventTime from t1 where t1.id = t2.id) where exists(select 1 from t1 where t1.id = t2.id)

注重效率