当前位置: 代码迷 >> Sql Server >> 求教一段连表 update 的 SQL语句,该怎么解决
  详细解决方案

求教一段连表 update 的 SQL语句,该怎么解决

热度:45   发布时间:2016-04-27 20:23:36.0
求教一段连表 update 的 SQL语句
求教:
两个表table1,table2,现在我要找出表2中   跟表1中“分数列”相等的第一个“姓名列”的值(根据“姓名列”排序),更新到表1中的col1字段,SQL该怎么写?

我大概写了一个如下,粗略表明一下我的意思,谁能帮忙改一下?谢谢

update   table1  
set   col1=   top   1   t2.姓名列
from   table1   t1,   table2   t2
where   t1.分数列=t2.分数列
order   by   t2.姓名列

------解决方案--------------------
update t1
set
col1=(select top 1 姓名列 from table2 where 分数列=t1.分数列)
from
table1 t1
------解决方案--------------------
update table1
set col1= t2.姓名列
from table1 t1, table2 t2
where t1.分数列=t2.分数列
and t2.姓名列=(select top 1 姓名列 from table2 where 分数列=t2.分数列 order by t2.姓名列)
  相关解决方案