读书人

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

发布时间: 2012-01-06 22:55:18 作者: rapoo

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

读书人网 >SQL Server

热点推荐