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)航:
           
           
          主站蜘蛛池模板: 井冈山市| 瑞丽市| 双峰县| 永登县| 安远县| 阳东县| 中西区| 宝鸡市| 泸西县| 黑水县| 福泉市| 遂昌县| 宣化县| 海晏县| 日照市| 五河县| 盘锦市| 喜德县| 东光县| 扶绥县| 大石桥市| 恩平市| 金山区| 荥经县| 菏泽市| 连州市| 镇巴县| 格尔木市| 晋宁县| 昭苏县| 基隆市| 南汇区| 庆元县| 溧阳市| 芜湖县| 杭锦旗| 静宁县| 卓尼县| 马山县| 孙吴县| 北辰区|