Chinaunix首页 | 论坛 | 博客
  • 博客访问: 558116
  • 博文数量: 63
  • 博客积分: 533
  • 博客等级: 中士
  • 技术积分: 1146
  • 用 户 组: 普通用户
  • 注册时间: 2012-09-24 17:56
文章分类

全部博文(63)

文章存档

2016年(1)

2014年(23)

2013年(17)

2012年(22)

分类: Windows平台

2014-05-14 15:23:05

我现在有两张表.A与B
A的字段有id,name,pwd...
B的字段有userid,password
现在我想把A中pwd更新成B中的password(A中的id与B中的userid都是相对应于学生的ID),SQL语句应该怎么写呢??



update A
     set pwd = (select password from B where userid = A.id);


 




这样写逻辑有问题的,, 如果对应的记录在B表中不存在,,会被更新成null的..

1. update A set pwd = (select password from B where userid = A.id); 
    where id in (select userid from B);

2. 如果B表的userid为主键的话,,可以使用关联更新..

update (
  select a.pwd pwd,b.password 
  from A,B
  where a.id = b.userid
)
set pwd = password;
阅读(1450) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~