新手求一条SQL语句怎么写,mssql

ATMCash4423 2013-07-10 02:37:37
有两个表
表a,有字段
ID(自增,key) 序号(唯一) 名称
1 ssd001 name1
2 ssd002 name2
3 ssd003 name3
4 ssd004 name4
5 ssd005 name5


表b,有字段
ID(自增,key) 序号(不唯一) 名称
1 ssd001 NULL
2 ssd001 NULL
3 ssd001 NULL
4 ssd004 NULL
5 ssd005 NULL

问表b如何根据序号在表a中查出序号相对应的名称,并把名称更新到表b中?

...全文
174 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
sinpoal 2013-07-10
  • 打赏
  • 举报
回复
update table_B b
set name=A.name
From table_B b
inner join table_A a
on a.index=b.index
唐诗三百首 2013-07-10
  • 打赏
  • 举报
回复
try this,

update ssd_history_day 
 set StationName = 
 (select top 1 StationName 
  from ssd_aws_station 
  where ssd_history_day.StationNum=ssd_aws_station.StationNum 
  and ssd_aws_station.StationType=1)
Andy__Huang 2013-07-10
  • 打赏
  • 举报
回复
update b set 名称=a.名称 from b inner join a on b.序号=a.序号
jmx123456789 2013-07-10
  • 打赏
  • 举报
回复
update ssd_history_day set StationName = (select StationName from ssd_aws_station where ssd_history_day.StationNum = ssd_aws_station.StationNum and ssd_aws_station.StationType = 1) 这语句应该返回一行 如果返回多行就报错
ATMCash4423 2013-07-10
  • 打赏
  • 举报
回复
引用 1 楼 shoppo0505 的回复:
update b set 名称 = a.名称 from a where b.序号 = a.序号
请问这句中有什么问题?在什么情况下会提示数据返回的数据不止一行? update ssd_history_day set StationName = (select StationName from ssd_aws_station where ssd_history_day.StationNum = ssd_aws_station.StationNum and ssd_aws_station.StationType = 1)
jmx123456789 2013-07-10
  • 打赏
  • 举报
回复
引用 1 楼 shoppo0505 的回复:
update b set 名称 = a.名称 from a where b.序号 = a.序号
+1
shoppo0505 2013-07-10
  • 打赏
  • 举报
回复
update b set 名称 = a.名称 from a where b.序号 = a.序号

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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