如何在mysql中按降序排序两个不同的日期列?

时间:2013-08-01 01:24:01

标签: mysql

有人可以帮我解决如何在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'关键字时,结果就会变得不正确。

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

试试这个......

order by CONCAT(cert_date,sptrain_from) desc;