表A、表B两个表,A表有一个时间字段timeA,B表有两个时间字段time1,time2,  ps(这两个时间字段有可能为NULL)  比较B表的两个时间字段选大的赋值给A表的timeA  求SQL语句 

解决方案 »

  1.   

    update a 
    set a.timeA=b.timeA
    from ta as a,tb as b where a.关联条件=b.关联条件
    and not exists(select 1 from tb where 关联条件=b.关联条件 and time1>b.time1)
    你可以先使用下面的语句看一下更新是否正确。select  a.timeA,b.timeA
    from ta as a,tb as b where a.关联条件=b.关联条件
    and not exists(select 1 from tb where 关联条件=b.关联条件 and time1>b.time1)
      

  2.   

    update A set timeA = case
        when time1 is null then time2
        when time2 is null then them1
        when time1 < time2 then time2
        else time1 end
    from A
    join B on A.id=B.id -- 假设A,B表以ID关联
      

  3.   


    update A set timeA = case
      when time1 is null then time2
      when time2 is null then them1
      when time1 < time2 then time2
      else time1 end
    from b
    where  A.关联关系=B.关联关系
      

  4.   


    update A set timeA = case
      when time1 is null then time2
      when time2 is null then them1
      when time1 < time2 then time2
      else time1 end
    from b
    where  A.关联关系=B.关联关系