首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2012-01-06 
求教一段连表 update 的 SQL语句求教:两个表table1,table2,现在我要找出表2中跟表1中“分数列”相等的第一个

求教一段连表 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.姓名列)

热点排行