【问题标题】:Compare two columns (date) and update new column with more recent date?比较两列(日期)并用最近的日期更新新列?
【发布时间】:2014-09-04 17:59:06
【问题描述】:

如何将日期列 foo 与其他日期列 bar 进行比较,并将列 foobar 更新为最近的日期。

ID        foo                    bar                foobar
1     '2014-01-23'         '0000-00-00'           '0000-00-00'
2     '0000-00-00'         '2013-03-01'           '0000-00-00'
3     '2013-03-03          '2014-04-04'           '0000-00-00'

【问题讨论】:

    标签: mysql date compare


    【解决方案1】:

    我认为你可以also 使用GREATEST

    UPDATE table_name
       SET foobar = GREATEST(foo,bar);
    

    【讨论】:

    • 我实际上最终使用了这个。所以我改变了我的投票。也认为它更清洁
    【解决方案2】:

    此语句根据case statement. 中的条件更新 foobar 试试这样的

    UPDATE table_name  
    SET     foobar =  CASE  
                            WHEN foo < bar THEN foo 
                            ELSE bar
                      END 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-19
      • 1970-01-01
      • 2019-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-25
      • 1970-01-01
      相关资源
      最近更新 更多