SQL中如何把A表的字段数据更新到B表的字段

来源:百度知道 编辑:UC知道 时间:2024/05/14 19:09:46
比如说有A,B两表。B表有5条数据,而A表的数据记录是灵活的。
互相可以关联的是a表的bnum字段与B表的bnum字段对应。
我现在想把B表中的qiantity字段更新到A表对应的记录的newqiantity字段。

这个SQL语句该如何实现??
急!!!!!

首先,你的问题就问的前后矛盾,标题描述是用A表某个字段更新B表的字段,举例子时又说用B表的qiantity更新A表的newqiantity?
假设A表有字段bnum和newqiantity,B表有字段bnum和qiantity,用B表的qiantity值更新对应的A表中的newqiantity值。以Oracle数据库为例:SQL语句如下:
update A set (newqiantity)=(select qiantity from B where A.bnum=B,bnum)

要实现两个表之间的数据的自动更新,可以采用触发器来实现,触发器的实现方法:
1、A表有数据更新的时候自动更新B表:
create or replace trigger tg after update on A for each row
begin
update B set xx=xx where b.aid = a.id;
end;
/
2、A表插入数据时,B表更新数据:
create or replace trigger tgg after insert on A for each row
begin
insert B values(xxxx);
end;
/
如果不需要每条数据都跟新,只需要在每次A更新的时候更新B,可以将后面的for each row去掉。

你在建表的时候,建立好这2个表之间的关系..我记得有3中选择...就是另外一个表的更新是否影响到和它关连的表.,

update A set newqiantity =
(select qiantity from B where A.bnum = B.bnum)

update
    (select A.bnum ,A.newqiantity,B.qiantity from A left join B on A.bnum=B.bnum) AS C
set C.newqiantity = C.qiantity
where C.bnum =XX