0

有人可以帮我解决如何在 mysql 中对两个不同的日期列进行排序吗?

我使用具有两个不同列的表创建了一个查询。第一个是 cert_date,另一个是 special_training_date_from。我想要做的是当我执行查询时,输出必须是这样的: cert_date 和 special_training_date_from 列必须按降序排序。例如,如果 cert_date 是 '2012-01-03, 2012-07-07' 而 special_training_date_from 是 '2011-05-03, 2013-08-01',则输出必须是:

    2013-08-01,
    2012-07-07,
    2012-01-03,
    2011-05-03

这是我使用的查询。

Select training_title, cert_date, special_training_date_from 
from tabletraining 
order by cert_date + sptrain_from desc;

每次我按升序排序时结果都是正确的,但我想按降序排序并且每次输入“desc”关键字时,结果都会不正确。

4

2 回答 2

0

尝试这个...

order by CONCAT(cert_date,sptrain_from) desc;
于 2014-02-12T08:43:10.867 回答
0

您可以将日期放在单个列中以使用order by

select training_title, thedate, which
from ((Select training_title, cert_date as thedate, 'cert' as which
       from tabletraining
      ) union all
      (select training_title, special_training_date_from, 'special' as which
       from tabletraining
      )
     ) t
order by thedate desc;

编辑:

如果您只想要两列中的不同日期,请使用:

select distinct thedate
from ((Select training_title, cert_date as thedate, 'cert' as which
       from tabletraining
      ) union all
      (select training_title, special_training_date_from, 'special' as which
       from tabletraining
      )
     ) t
order by thedate desc;
于 2013-08-01T01:27:32.460 回答