基于年份和年份+月份的排序表

时间:2012-10-25 14:42:13

标签: mysql sorting

我有一个包含列Year和ID

的表
YEAR              ID
1988                29
1989                 89
1990                22
1992                9
1994                8
1998                23
1922                 20
August 1990          12
September 2009         14
August 1991            11
November 2009           33
October 1990            30
January 1990            55
March 2001              24

我有没有办法按照我的最终结果排序的方式对表格进行排序..我正在寻找像

这样的结果
YEAR              ID
1922                20
1988                29
1989                 89
1990                22
1992                9
1994                8
1998                23
January 1990            55
August 1990          12
October 1990            30
August 1991            11
March 2001              24
September 2009         14
November 2009           33

提前谢谢

1 个答案:

答案 0 :(得分:1)

只需将myYear替换为您的表名...

select year,id
from
(select year,id,
case when STR_TO_DATE(year,'%Y') is not null then STR_TO_DATE(year,'%Y') else STR_TO_DATE(year,'%M %Y') end as d,
case when STR_TO_DATE(year,'%Y') is not null then 0 else 1 end as ob
from myYear
) y
order by ob asc,d asc;