那就更不对了,update tab2 set num=(select max(num) from tab1 where tab1.id=tab2.id) where exists (select 1 from tab1 where tab1.id=tab2.id);就可以了
update tab2 set num=(select num from tab1 where tab1.id=tab2.id) 以上语句要是tab2表中要是存在tab1.id<>tab2.id记录,该记录原来的num就会 被空覆盖掉 update tab2 set num=(select num from tab1 ) where tab1.id=tab2.id 以上语句子查询中返回的是多行记录,所以语句是错误的。 lee_billiy(思思)写的是可以的 update tab2 set num=(select max(num) from tab1 where tab1.id=tab2.id) where tab1.id=tab2.id
set 后面的值只要唯一就可了。
如果楼主的两个表的id都是PK,那也没有什么错误,语法上应该可以实现.
我可能没描述清楚。 举例: 有2张表[商品库存] [销售明细] 商品库存:ID NAME sell_num 销售明细:ID NAME num amt --------------------------------- 由于程序出错,"商品库存"的销售数量(sell_num)出错,需要从"销售明细"中重新统计销售数量,来更新sell_num。 select id,sum(num) into :vid,:vnum from 销售明细 group bu id; update 商品库存 set sell_num=:vnum where id=:vid; ======================== 我不想用过程语言,用单个UPDATE可以吗?
应该是这样: update tab2 set num=b.num from tab1 a inner jion tab2 b on a.id=b.id 提醒:编码规范
以上语句要是tab2表中要是存在tab1.id<>tab2.id记录,该记录原来的num就会
被空覆盖掉
update tab2 set num=(select num from tab1 )
where tab1.id=tab2.id
以上语句子查询中返回的是多行记录,所以语句是错误的。
lee_billiy(思思)写的是可以的
update tab2 set num=(select max(num) from tab1 where tab1.id=tab2.id)
where tab1.id=tab2.id
举例:
有2张表[商品库存] [销售明细]
商品库存:ID NAME sell_num
销售明细:ID NAME num amt
---------------------------------
由于程序出错,"商品库存"的销售数量(sell_num)出错,需要从"销售明细"中重新统计销售数量,来更新sell_num。
select id,sum(num) into :vid,:vnum from 销售明细 group bu id;
update 商品库存 set sell_num=:vnum where id=:vid;
========================
我不想用过程语言,用单个UPDATE可以吗?
update tab2
set num=b.num
from tab1 a inner jion tab2 b on a.id=b.id
提醒:编码规范