有两张表
A
  商品 价格
   NO1   2
   NO2  1.5
   NO3  1.8
   NO4  1.4
B
  商品  价格   NO1   2
   NO1   2.5
   NO4  1.4
   NO4  2.1
   NO2  2.9
求一条语句,用B表中每种商品的价格最大值去更新A表中每种相同物料的价格

解决方案 »

  1.   

    update a
    set 价格=b.价格
    from 
    (select 商品,max(价格)价格 from b group by  商品) b
    where
    a.商品=b.商品
      

  2.   

    update a
    set 价格=(select max(价格) from b where 商品=a.商品)
      

  3.   

    --try
    update A
    set A.价格 = c.价格
    from A
    inner join (select 商品, max(价格)as 价格 from B group by 商品) c on a.商品 = A.商品
      

  4.   

    update a set 价格=(select max(价格) from b where 商品=a.商品 and a.价格<b.价格)
      

  5.   

    -->生成测试数据
     
    declare @A table([商品] nvarchar(3),[价格] decimal(18,1))
    Insert @A
    select N'NO1',2 union all
    select N'NO2',1.5 union all
    select N'NO3',1.8 union all
    select N'NO4',1.4
    --Select * from @A
    declare @B table([商品] nvarchar(3),[价格] decimal(18,1))
    Insert @B
    select N'NO1',2 union all
    select N'NO1',2.5 union all
    select N'NO4',1.4 union all
    select N'NO4',2.1 union all
    select N'NO2',2.9
    --Select * from @B
    update a set 价格=isnull((select max(价格) from @B where 商品=a.商品),价格)
    from @A aselect * from @A
    /*
    商品   价格
    ---- ---------
    NO1  2.5
    NO2  2.9
    NO3  1.8
    NO4  2.1(4 row(s) affected)*/
      

  6.   

    update a
    set 价格=b.价格
    from 
    (select 商品,max(价格) 价格 from b group by  商品) b
    where
    a.商品=b.商品