Selecting most recent date between two columns

前端 未结 13 1415
再見小時候
再見小時候 2020-12-30 22:15

If I have a table that (among other columns) has two DATETIME columns, how would I select the most recent date from those two columns.

Example:

13条回答
  •  星月不相逢
    2020-12-30 23:09

    All other correct answers as already posted.

    But if you are still really looking for MAX keyword then here is a way :

    select ID , MAX(dt) from 
    (  select Id , Date1 as dt from table1
       union  
       select ID , Date2 from table2
    ) d
    group by d.Id
    

提交回复
热议问题