假设:A表:
id    name    year
01    张飞    18
02    杨秀    21
03    萨尔    22B表:
id    score
01    90
02    88
03    69问题:
凡是A表中year大于20的,对应的B表中的score加上10分。(变成:张飞90,杨秀98,萨尔32)
这句SQL语句怎么写呢?

解决方案 »

  1.   

    Update B
    set score = score +10 
    where exists (select 1 from A where id = B.id and year >20 )
      

  2.   

    select name,case when a.year > 20 then b.score +10 else b.score end from a,b where a.id = b.id
      

  3.   


    "select 1 "是什么意思啊?为什么是select 1?
      

  4.   

    create table A(id varchar(10),name varchar(10),[year] int)
    insert into a values('01', '张飞', 18)
    insert into a values('02', '杨秀', 21)
    insert into a values('03', '萨尔', 22)
    create table B(id varchar(10),score int)
    insert into b values('01', 90)
    insert into b values('02', 88)
    insert into b values('03', 69)
    go--查询
    select b.id , score = (case when a.[year] > 20 then b.score + 10 else b.score end) from a , b where a.id = b.id
    /*
    id         score       
    ---------- ----------- 
    01         90
    02         98
    03         79(所影响的行数为 3 行)
    */--更新
    update b set score = score + 10 from a , b where a.id = b.id and a.[year] > 20select * from b
    /*
    id         score       
    ---------- ----------- 
    01         90
    02         98
    03         79(所影响的行数为 3 行)
    */drop table a , b