主题:【求助】SQL怎样用一个表中的信息来更新另外一个表 -- 铁手

2016-04-20 15:32:38四四方方
试试

create table t1 (

id number(2),

point number(4)

)

;

create table t2 (

id number(2),

total number(4)

)

;

insert into t1 values (12, 1);

insert into t1 values (12, 2);

insert into t1 values (12, 3);

insert into t1 values (12, 4);

insert into t1 values (13, 11);

insert into t1 values (13, 12);

insert into t1 values (13, 13);

insert into t1 values (13, 14);

select * from t1;

=========

id point

=========

12 1

12 2

12 3

12 4

13 11

13 12

13 13

13 14

insert into t2 values (12, 0);

insert into t2 values (13, 0);

select * from t2;

=========

id total

=========

12 0

13 0

update t2 a set total = (select sum(point) from t1 b where b.id = a.id group by a.id);

select * from t2;

=========

id total

=========

12 10

13 50

drop table t1 purge;

drop table t2 purge;

帖:4190906 复 4189313
帖内引用