ORDER BY两个日期mysql查询


ORDER BY two dates mysql query

我要在查询中下订单,比如:

SELECT * FROM `sales_report` ORDER BY `cancel_date`, `pur_date`

但它不起作用,你能检查一下这个查询中有什么问题吗,因为它没有订购日期?

pur_date类型是date,cancel_date类型为text,所以它有问题吗?我不能改变它的类型。

我回应了一个不同的想法,比如:

if(cancel_date != ''){
$transection_date = cancel_date;
}
else {
$transection_date = pur_date;
}

尝试使用STR_TO_DATE()函数:

SELECT
    *
FROM
    `sales_report`
ORDER BY
    STR_TO_DATE(`cancel_date`, '%Y-%m-%d'), `pur_date`;

正如zerkms在注释中提到的那样:您应该使用格式说明符,这取决于cancel_date列中的文本值。

SELECT *, CAST(`cancel_date` as DateTime) cancelDate 
FROM `sales_report`
ORDER BY `cancelDate`, `purDate`

这应该工作