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 閱讀(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)
            這樣也可以,在面試的時候這樣的思路應該是比較容易想到的呀,呵呵  回復  更多評論   
          • # 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)  回復  更多評論   

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


          網站導航:
           
           
          主站蜘蛛池模板: 澜沧| 江津市| 西乌| 龙山县| 通州市| 云阳县| 西贡区| 敦煌市| 宁城县| 中江县| 鄄城县| 黔西县| 商水县| 日喀则市| 华宁县| 阳谷县| 白水县| 广汉市| 衡阳县| 磴口县| 郸城县| 大厂| 南通市| 曲松县| 牙克石市| 疏附县| 林口县| 工布江达县| 新化县| 揭西县| 威海市| 贡山| 阿荣旗| 广宗县| 茶陵县| 永定县| 濮阳市| 白山市| 增城市| 金华市| 彩票|