2个表a表
a1 a2b表
b1 b2a2和b2是一样的,怎么从b表取b1到a1写了2种语句update a
set a1=b.b1 
where b.b2=a.a2
报错:列前缀'b'与查询中所用的表名或别名不匹配update a
set a1=(select b1 from a,b where b.b2=a.a2)
报错:子查询返回的值多于一个真的迷糊了,还请各位DX帮忙看下。

解决方案 »

  1.   

    update a
    set a1=b.b1 
    where b.b2=a.a2
    改為
    update a
    set a1=b.b1 
    from a, b
    where b.b2=a.a2
      

  2.   

    报错:子查询返回的值多于一个說明表中數據不是一一對應的,而是一對多的,如果是隨便抓一個b1
    update a
    set a1=(select b1 from a,b where b.b2=a.a2)
    改為
    update a
    set a1=(select Max(b1) from b where b2=a.a2)
      

  3.   

    update a
    set a1=b.b1 
    from a,b
    where b.b2=a.a2update a
    set a1=(select top 1 b1 from b where b2=a.a2)
      

  4.   

    update a
    set a1=b.b1 
    from b
    inner join a
    on b.b2=a.a2update a
    set a1=(select b1 from a,b where b.b2=a.a2)--查询返回的b1不止一个
      

  5.   

    --eg:Create Table a
    (a1 Int,
     a2 Int)
    Insert a Select 0, 1
    Union All Select 0, 2Create Table b
    (b1 Int,
     b2 Int)
    Insert b Select 11, 1
    Union All Select 22, 2
    GO
    update a
    set a1=b.b1 
    from a, b
    where b.b2=a.a2Select * From a
    GO
    Drop Table a, b
    --Result
    /*
    a1 a2
    11 1
    22 2
    */
      

  6.   

    --方法二--eg:Create Table a
    (a1 Int,
     a2 Int)
    Insert a Select 0, 1
    Union All Select 0, 2Create Table b
    (b1 Int,
     b2 Int)
    Insert b Select 11, 1
    Union All Select 22, 2
    GO
    update a
    set a1=(select Max(b1) from b where b2=a.a2)Select * From a
    GO
    Drop Table a, b
    --Result
    /*
    a1 a2
    11 1
    22 2
    */
      

  7.   

    update a
    set a1=(select Max(b1) from b where b2=a.a2)
      

  8.   

    update a
    set a1=b.b1 
    where b.b2=a.a2
    报错:列前缀'b'与查询中所用的表名或别名不匹配--------->对应的select 语句是
    select * from a where b.b2=a.a2 这当然有错了.好的建议:
    在update之前,最好先select.
      

  9.   

    lt1129(修理地球) ( ) 信誉:100    Blog   加为好友  2007-07-13 10:56:41  得分: 0  
     
     
       update a
    set a1=(select Max(b1) from b where b2=a.a2)  
     
    ----------
    怎麼老是復制?
      

  10.   

    谢谢了,我用的是第1个语句。
    一直都是单表做UPDATE,多表很少做。
    又学到了,呵呵。
      

  11.   

    ao(皮卡丘) ( ) 信誉:100    Blog   加为好友  2007-07-13 11:02:49  得分: 0  
     
     
       谢谢了,我用的是第1个语句。
    一直都是单表做UPDATE,多表很少做。
    又学到了,呵呵。
      
     
    ----------
    用第一個的效率更優些。 :)