SQL更新多条记录

funyunnetwork 2009-11-08 10:25:53
A表
字段 a1 a2 a3
1 2 3
6 2 7
B表
字段 b1 b2 b3
4 5 3
8 9 7
我想将 a3值=b3值并且a2=2的记录的b1值更新到a1中(必须一一对应) 应该如何更新
sql = "Update A Set A.a1 = (select B.b3 from B,A where A.a3=B.b3 and A.a2='2') where A.a2='2'"
为什么不能更新多条记录
也就是说 我想将a1字段中的1更新为4 6更新为8
如果查询结果只有一条记录 可以正常更新 如果记录数大于1,则不能更新,如何解决!~~
...全文
244 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
funyunnetwork 2009-11-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 luoyoumou 的回复:]
SQL codedroptable a;droptable b;createtable a(a1int, a2int, a3int);insertinto a(a1,a2,a3)select1,2,3unionallselect6,2,7;createtable b(b1int, b2int, b3int);insertinto b(b1,b2,b3)select4,5,3unionallsele?-
[/Quote] 又测试了一下 .... OK了 谢!~~
funyunnetwork 2009-11-08
  • 打赏
  • 举报
回复
update A set A.a1=(select B.b1 from A,B where A.a3=B.b3 and A.a2=2)
错误提示:操作必须使用一个可更新的查询

funyunnetwork 2009-11-08
  • 打赏
  • 举报
回复
不好意思 楼上的解答,我都试过 一个都没成功, 还有 AB表已经存在,不需要建表,我只想把符合条件的字段值更新到A表
--小F-- 2009-11-08
  • 打赏
  • 举报
回复
update
a
set
a.a1=b.b1
from
a,b
where
a.a3=b.b3 and a.a2=2
luoyoumou 2009-11-08
  • 打赏
  • 举报
回复

drop table a;
drop table b;

create table a(a1 int, a2 int, a3 int);
insert into a(a1,a2,a3)
select
1,2,3 union all select
6,2,7;

create table b(b1 int, b2 int, b3 int);
insert into b(b1,b2,b3)
select
4,5,3 union all select
8,9,7;

update a
set a1=b.b1
from a, b
where a.a3=b.b3 and a.a2=2;

select * from a;

select a.a1, a.a2, a.a3,
b.b1, b.b2, b.b3
from a, b
where a.a3=b.b3;
sgtzzc 2009-11-08
  • 打赏
  • 举报
回复
update a
set a.a1=b.b1
from a,b
where a.a3=b.b3 and a.a2=2
SQL77 2009-11-08
  • 打赏
  • 举报
回复
UPDATE A SET A1=B1 FROM B WHERE A.A3=B.B3 AND A.A2=2

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧