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
          看到這題目讓我傻了,這么簡單的題目,可我就是不會。

          下面是我回來后重新查幫助后才解決的。測試數(shù)據(jù)如下,
          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)

          總結(jié):update 還可以和 from 一起用,自己真的是孤陋寡聞。

          posted on 2006-12-14 19:25 pear 閱讀(602) 評論(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)
            這樣也可以,在面試的時候這樣的思路應(yīng)該是比較容易想到的呀,呵呵  回復(fù)  更多評論   
          • # re: 傷心太平洋,筆試栽跟頭。
            pear
            Posted @ 2006-12-15 07:44
            @Vista
            不知 sqlserver 有沒有rownum 這個變量:
            你的語句我改了一下,跑出的結(jié)果是正確的。看來只能怪自己少見多怪,呵呵。
            update main set salary = ( select salary from test where main.test_id = test.id)  回復(fù)  更多評論   

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
           
          主站蜘蛛池模板: 永定县| 板桥市| 休宁县| 常山县| 清镇市| 滦南县| 林西县| 富川| 清苑县| 安新县| 毕节市| 贵定县| 浮梁县| 航空| 石渠县| 营口市| 清河县| 福泉市| 静乐县| 定安县| 金华市| 高邑县| 株洲县| 张家川| 潍坊市| 榆林市| 卢龙县| 武川县| 广丰县| 达州市| 曲靖市| 黎平县| 东莞市| 江永县| 芦溪县| 临海市| 高清| 射洪县| 翁源县| 高唐县| 怀安县|