test表
 id      salary 
1       100   
2       200    

main表
id    test_id    salary       
1     1         Null      
2     1         Null       
3     2          Null           

请用test表中的salary更新main表中的salary
看到这题目让我傻了,这么简单的题目,可我就是不会。

下面是我回来后重新查帮助后才解决的。测试数据如下,
create table test(
  id int primary key identity(1,1),
  salary int
)
go
insert into test(salary) values(100)
insert into test(salary) values(200)
go
create table main(
  id int primary key identity(1,1),
  test_id int references test(id),
  salary int
)
go
insert into main(test_id) values(1)
insert into main(test_id) values(2)
go

答案:
UPDATE main
SET main.salary = test.salary
FROM main INNER JOIN test ON (test.id =main.test_id)

总结:update 还可以和 from 一起用,自己真的是孤陋寡闻。

posted on 2006-12-14 19:25 pear 阅读(584) 评论(2)  编辑  收藏
Comments
  • # re: 伤心太平洋,笔试栽跟头。
    Vista
    Posted @ 2006-12-15 01:32
    update main set salary = ( select salary from test where main.test_id = test.test_id and rownum <=1)
    这样也可以,在面试的时候这样的思路应该是比较容易想到的呀,呵呵  回复  更多评论   
  • # re: 伤心太平洋,笔试栽跟头。
    pear
    Posted @ 2006-12-15 07:44
    @Vista
    不知 sqlserver 有没有rownum 这个变量:
    你的语句我改了一下,跑出的结果是正确的。看来只能怪自己少见多怪,呵呵。
    update main set salary = ( select salary from test where main.test_id = test.id)  回复  更多评论   

只有注册用户登录后才能发表评论。


网站导航: