SQL3个表连接修改字段

来源:百度知道 编辑:UC知道 时间:2024/06/28 04:29:28
第一个表 有 id,no 其中id已有数据,需要修改no
第二个表 有 id 和center字段,已有数据
第三个表 有 center和no字段,已有数据

现在要用一个update语句,通过第一张表的id 找到第二张表center,再通过center找到第三张表的no填写到第一张表中。。。
第一位,select返回的是一组数据不是一个数据,会报:
服务器: 消息 512,级别 16,状态 1,行 1
子查询返回的值多于一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。
语句已终止。

第二位 我用的是sql2000.。。

update 表1
set no = (select c.no
from 表2 b,表3 c
where b.center = c.center
and 表1.no = b.no)

--你数据有问题。换个写法吧,给你取最大值
update 表1
set no = (select max(c.no)
from 表2 b,表3 c
where b.center = c.center
and 表1.no = b.no)

楼上的代码把我看晕了,直接联接更新就行了
update 表1
set no=表3.no
from 表1,表2,表3
where 表1.id=表2.id and 表2.center=表3.center

merge into 第一个表 a
using (select no,id from 第三个表,第二个表 where 第二个表.center=第三个表.center) b on (a.id=b.id)
when MATCHED then
update set a.no=b.no

可以的,类似这种UPDATE A a , B b SET a.col1= 'xxx' , b.col1 = 'xxx' WHERE a.col2 = 'xxxx' and b.col2 = 'xxxxx';

update t
set t.no=表3.no
from 表1 t,表2,表3
where t.id=表2.id and 表2.center=表3.center