我正在尝试对mysql查询中的列进行排序,该列的类型为varchar,但可能/可能没有数值。
for example it may have the values
2012-10
2012-41
2012-1
which should be sorted as follows:
2012-1
2012-10
2012-41
but if the values are :
M-1
M-13
M-5
it should be sorted as :
M-1
M-5
M-13
and if null values are present it should be last.不知道这是否可能。请帮帮忙
发布于 2013-08-06 07:53:47
如果要按连字符后面的数字排序,请使用以下命令:
ORDER BY column IS NOT NULL, SUBSTRING_INDEX(column, '-', 1), CAST(SUBSTRING_INDEX(column, '-', 2) AS DECIMAL)column IS NOT NULL对空列进行最后排序,CAST()表达式按数字对其余列进行排序。
https://stackoverflow.com/questions/18074423
复制相似问题