SQL时间列不按升序排序

时间:2015-01-10 05:28:02

标签: mysql sql time sql-order-by

我在尝试编写按时间列排序的SQL语句时遇到了一些麻烦,该列以varchar格式按升序排列。这是我的SQL语句:

SELECT mrtpopTime, mrtpopAmt
FROM tm_mrtpop
WHERE mrtpopName = ''
ORDER BY mrtpopTime

我得到了这些结果:

enter image description here

从图片中可以看出,它按字符排序。无论如何要对它进行排序:

0:00, 1:00, 2:00, 3:00 all the way to 23:00

有什么想法吗?提前致谢。

2 个答案:

答案 0 :(得分:3)

将varchar time列转换为time(或虚拟日期加时间)并按顺序排序:

SELECT mrtpopTime, mrtpopAmt
FROM tm_mrtpop
WHERE mrtpopName = ''
ORDER BY STR_TO_DATE(mrtpopTime, '%H:%i')

答案 1 :(得分:2)

由于mrtpopTime在varchar中,因此不会排序。因此将其投入浮动并按

排序
SELECT  mrtpopTime, mrtpopAmt
FROM tm_mrtpop
WHERE mrtpopName = ''
ORDER BY 'cast(mrtpopTime as float) time'