日期:2014-05-18  浏览次数:20394 次

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

------解决方案--------------------
SQL code
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)

------解决方案--------------------
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关联

------解决方案--------------------
SQL code

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.关联关系

------解决方案--------------------
SQL code

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.关联关系