【问题标题】:mysql select with changing date based on columnmysql 根据列选择更改日期
【发布时间】:2015-02-20 05:03:32
【问题描述】:

我有一个订单表,有两种类型的订单。 “修复”和“新”。所有订单都有一个 orderDate。我需要以一种不寻常的方式对 mysql 选择的结果进行排序。

如果订单类型为“修复”,则订单日期加上 1 天的到期日。

如果订单类型为“新”,则订单日期加上 2 天的到期日。

我需要在截止日期之前订购,但截止日期是列中不存在的东西。我该怎么做?

【问题讨论】:

    标签: mysql select


    【解决方案1】:

    正如我从您那里看到的问题,您需要在截止日期之前订购 这是due_dateenter code here的代码排序:

    SELECT 
        *
    FROM
        tablename
    ORDER BY CASE order_type
        WHEN 'fix' THEN order_date + INTERVAL 1 DAY
        ELSE order_date + INTERVAL 2 DAY
    END DESC;
    

    【讨论】:

      【解决方案2】:

      您可以根据该条件和按该列的顺序生成一个虚拟列:

      SELECT
           CASE order_type
               WHEN 'fix' THEN datecolumn + INTERVAL 1 DAY
               ELSE datecolumn + INTERVAL 2 DAY END order_date
      FROM
          tablename
      ORDER BY order_date DESC
      

      【讨论】:

        【解决方案3】:

        case 语句与date_add() 函数一起使用:

        select type, case order.type
                                when 'fix' then date_add(OrderDate,interval 1 DAY)
                                else then date_add(OrderDate,interval 2 DAY)
                           end order_date
        from table_name
        order by order_date
        

        【讨论】:

          猜你喜欢
          • 2012-10-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-07-25
          • 1970-01-01
          • 2019-02-14
          • 2020-01-20
          • 1970-01-01
          相关资源
          最近更新 更多