问题描述:
表A(id_a, x)
数据:1, 0表B(id_b, y)
数据:1, 2
数据:1, 4
数据:1, 9
      现要做类似如下更新:UPDATE A set x = x + sum(y) from B where id_a=id_b当然,以上sql语句是错误的,执行后表A的数据应为:1,15这个sql语句应该怎么写?

解决方案 »

  1.   

    UPDATE TA
    SET x = x + TB.y
    FROM TA
    INNER JOIN (SELECT id_b, SUM(y) AS y FROM B GROUP BY id_b)TB
    ON TA.id_a = TB.id_b
      

  2.   


    UPDATE A set x = x + b.y from A,
      (Select id_b,sum(y) as y from B group by id_b) as b where a.id_a=b.id_b
      

  3.   

    update A set x=x+y from (select id_b,sum(y) as y from B group by id_b) B where id=B.id_b
      

  4.   

    update A set x=x+B.y from (select id_b,sum(y) as y from B group by id_b) B where id=B.id_b
      

  5.   

    UPDATE A set x = x + (select isnull(sum(y),0) from B where id_a=A.id_b)
      

  6.   

    update A set  x=a.x+b.y  from A a,(Select id_b,sum(y) as y from B group by id_b) as b where a.id_a=b.id_b
      

  7.   

    create table A(id_a int,x int)
    insert into a select 1, 0
    create table B(id_b int,y int)
    insert into b select 1, 2
    union select 1, 4
    union select 1, 9
    update A set x=x+B.y from (select id_b,sum(y) as y from B group by id_b) B where id_a=B.id_b
    select * from a
    --------------------result
    id_a     x
    1 15
      

  8.   

    create table A(id_a int,x int)
    go
    insert into a select 1, 0
    go
    create table B(id_b int,y int)
    go
    insert into b select 1, 2
    union select 1, 4
    union select 1, 9
    gowith temp
    as
    (
    select sum(y) as y,id_b
    from B
    group by id_b
    )
    update A
    set x = x + y
    from A tempA
    inner join temp tempB
    on id_a = tempB.id_b 
    go
    select * from A
      

  9.   


    create table A(id_a int, x int)
    insert A select 1, 0
    go
    create table B(id_b int, y int)
    insert B select 1, 2
    insert B select 1, 4
    insert B select 1, 9
    goupdate A set x=B.y
    from
    (
    select id_b, y=sum(y)
    from B
    group by id_b
    )as B
    where A.id_a=B.id_bselect * from A--result
    id_a        x           
    ----------- ----------- 
    1           15(1 row(s) affected)
      

  10.   

    --应该是这样update A set x=x+B.y
    from
    (
    select id_b, y=sum(y)
    from B
    group by id_b
    )as B
    where A.id_a=B.id_b
      

  11.   

    create table A(id_a int,x int)
    insert into a select 1, 0create table B(id_b int,y int)
    insert into b select 1, 2
    union select 1, 4
    union select 1, 9update a
    set x = t.y
    from a, (select id_b,sum(y) as y from b group by id_b) t
    where a.id_a = t.id_bselect * from adrop table a,b--result
    id_a        x           
    ----------- ----------- 
    1           15(所影响的行数为 1 行)
      

  12.   

    UPDATE TA
    SET x = x + TB.y
    FROM TA
    INNER JOIN (SELECT id_b, SUM(y) AS y FROM B GROUP BY id_b)TB
    ON TA.id_a = TB.id_b
      

  13.   

    UPDATE A set x = x + (select isnull(sum(y),0) from B where id_a=A.id_b)
      

  14.   

    create table A(id_a int,x int )
    insert A select 1, 0create table B(id_b int, y int)
    insert B select 1, 2
    union select 1, 4
    union select 1, 9
          
    select * from A
    select * from Bupdate A set x = x + (select sum(y) from B where A.id_a = B.id_b)
      

  15.   

    UPDATE A SET X = X + (SELECT SUM(Y) FROM B WHERE A.ID_A = B.ID_B)一句话就可以了,
      

  16.   

    use mySQL
    go
    if object_id('t') is not null
    drop table t
    go
    create table t(id_a int, x int)
    insert t select 1,0
    union all select 2,2
    go
    if object_id('t1') is not null
    drop table t1
    go
    create table t1(id_b int,y int)
    insert t1 select 1,2
    union all select 1,4
    union all select 1,9
    union all select 2,5
    union all select 2,1
    union all select 2,10
    go
    select * from t
    select * from t1
    go
    update t 
    set x = x + (select sum(y) from t1 where t.id_a = t1.id_b)
    go
    select * from t
      

  17.   

    update a set x=(x+(select sum (y)from b as y)) from b where id_a=id_b
      

  18.   

    学习中... 欢迎加入ASP.NET(C#)学习交流QQ群号:32801051
      

  19.   


    update a set x =x +(select sum(y) where id_a=id_b)
      

  20.   


    declare @A table(id_a int,x int)
    declare @B table(id_b int,y int)
    /*********@A**********/
    insert into  @A
    select 1,0
    /*********@A**********/
    /*********@B**********/
    insert into  @B
    select 1,2
    union all
    select 1,4
    union all
    select 1,9
    /*********@B**********/
    select * from @A
    select * from @Bupdate @A set x=x+b.s
    from @A a left join (select id_b,sum(y) as s from @B where id_b=1 group by id_b) b
    on a.id_a=b.id_b
    select * from @A
      

  21.   

    不好意思刚才发的是没有整理的,这回的好一些
    declare @A table(id_a int,x int)
    declare @B table(id_b int,y int)
    /*********@A**********/
    insert into  @A
    select 1,0
    /*********@A**********/
    /*********@B**********/
    insert into  @B
    select 1,2
    union all
    select 1,4
    union all
    select 1,9
    /*********@B**********/update @A set x=x+b.s
    from @A a left join (select id_b,sum(y) as s from @B group by id_b) b
    on a.id_a=b.id_b
    select * from @A
      

  22.   

    update a 
    set a=t.a from (select a.id_a,sum(b) a from a ,b where a.id_a=b.id_b group by a.id_a )t,a
    where a.id_a=t.id_a
      

  23.   

    update a set x =x +(select sum(y) where id_a=id_b)
      

  24.   

    UPDATE A set x = x +(select sum(y) from B where id_a=id_b)
      

  25.   

    update A set x = x + (select sum(Y) from B where B.ID_B=A.ID_A)