时间字段1 时间字段2,这两个字段只有一个有时间,时间字段1有数据,时间字段2就没有,反之亦然,如何实现按时间大小排序(字段1有时间就按字段1,2也是)
按时间逆序排序,执行如下sql:
SELECT t2.id,time1,time2 from testB t2
join
( SELECT id,time1 as c from testB where time2 is NULL
UNION
SELECT id,time2 as c from testB where time1 is NULL
ORDER BY c DESC
) t1
on t2.id=t1.id ORDER BY t1.c DESC
select * from test
order by isnull(time1,time2) desc