【发布时间】:2015-02-20 05:03:32
【问题描述】:
我有一个订单表,有两种类型的订单。 “修复”和“新”。所有订单都有一个 orderDate。我需要以一种不寻常的方式对 mysql 选择的结果进行排序。
如果订单类型为“修复”,则订单日期加上 1 天的到期日。
如果订单类型为“新”,则订单日期加上 2 天的到期日。
我需要在截止日期之前订购,但截止日期是列中不存在的东西。我该怎么做?
【问题讨论】:
我有一个订单表,有两种类型的订单。 “修复”和“新”。所有订单都有一个 orderDate。我需要以一种不寻常的方式对 mysql 选择的结果进行排序。
如果订单类型为“修复”,则订单日期加上 1 天的到期日。
如果订单类型为“新”,则订单日期加上 2 天的到期日。
我需要在截止日期之前订购,但截止日期是列中不存在的东西。我该怎么做?
【问题讨论】:
正如我从您那里看到的问题,您需要在截止日期之前订购
这是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;
【讨论】:
您可以根据该条件和按该列的顺序生成一个虚拟列:
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
【讨论】:
将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
【讨论】: