今天上午UML上机课时偷上了QQ~~~~
oracle社区群里有人问:
a b
id name sex id name age sex
1 ww m 1 ww 18
2 kk w 2 kk 21
3 cc m 3 cc 19
怎么把表a中sex字段的值复制到b?
该题解题思路如下:
一、建表a,b,插入两行数据
create table a(
id smallint primary key,
name varchar2(20),
sex varchar2(2)
);
create table b(
id smallint primary key,
name varchar2(20),
age smallint,
sex varchar2(2)
);
insert into a values(1,'ww','m');
insert into a values(2,'kk','w');
insert into b values(1,'ww',20,'');
insert into b values(2,'kk',19,'');
insert into c select * from a;
2.若表字段不同,如将a中的数据插入b中
insert into b select id,name,null,sex from a;
三、将a表中的sex字段插入到b表中
update b set b.sex = (select sex from a where a.id = b.id);
或update b set b.sex = (select sex from a where a.id = b.id)where exists(select 1 from a where a.id = b.id);