test表
          ?id????? salary?
          1?????? 100???
          2??? ?? 200????

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

          請(qǐng)用test表中的salary更新main表中的salary
          看到這題目讓我傻了,這么簡單的題目,可我就是不會(huì)。

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

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


          網(wǎng)站導(dǎo)航:
           
           
          主站蜘蛛池模板: 磐石市| 安阳县| 安阳市| 共和县| 焉耆| 休宁县| 伽师县| 无棣县| 西城区| 平顺县| 宁国市| 昌吉市| 新宾| 泉州市| 原阳县| 抚州市| 安福县| 横峰县| 隆林| 砚山县| 德阳市| 扬中市| 林西县| 郁南县| 沛县| 海原县| 英山县| 和顺县| 高州市| 微博| 沈丘县| 乐都县| 衡南县| 容城县| 新宁县| 宜良县| 体育| 福清市| 长丰县| 巩义市| 汽车|