按日期类型的问题排序

时间:2011-02-03 16:58:47

标签: mysql

与'01 -01-2011'和'31 -01-2011'之间的date_format(ExpenditureDate,'%d-%m-%Y')相同,

此外,我无法检查之间的情况,????

支出日期 - 日期类型enter image description here

以下查询正确显示,最新日期将逐一显示

SELECT ExpenditureName,ExpenditureAmount,ExpenditurePlaceTypeWhome,ExpenditureDate FROM tblexpenditure WHERE Status=1 AND EntryUser=2 ORDER BY ExpenditureDate DESC 

但如果我将date_format(ExpenditureDate,'%d-%m-%Y')添加为ExpenditureDate

然后松开我的正确顺序,显示无序

SELECT ExpenditureName,ExpenditureAmount,ExpenditurePlaceTypeWhome,date_format(ExpenditureDate,'%d-%m-%Y') as ExpenditureDate FROM tblexpenditure WHERE Status=1 AND EntryUser=2 ORDER BY ExpenditureDate DESC

3 个答案:

答案 0 :(得分:2)

我没有运行mysql的盒子来测试,但我尽量避免命名字符串格式化可显示的'支出日期'与你排序的相同。

答案 1 :(得分:2)

他们可能会排序,但按日期的词典顺序而不是按时间顺序排序。这是正常的,因为您对ExpenditureDate别名进行排序,这是一个包含格式化日期的字符串。 使用另一个别名:

SELECT ExpenditureName,ExpenditureAmount,ExpenditurePlaceTypeWhome,date_format(ExpenditureDate,'%d-%m-%Y') as FormattedExpenditureDate FROM tblexpenditure WHERE Status=1 AND EntryUser=2 ORDER BY ExpenditureDate DESC

如果你真的想保留相同的别名,你也可以试试这个:

SELECT ExpenditureName,ExpenditureAmount,ExpenditurePlaceTypeWhome,date_format(ExpenditureDate,'%d-%m-%Y') as ExpenditureDate FROM tblexpenditure WHERE Status=1 AND EntryUser=2 ORDER BY tblexpenditure.ExpenditureDate DESC

答案 2 :(得分:1)

MySQL似乎对格式化值进行排序。试着写

...,date_format(ExpenditureDate,'%d-%m-%Y') as FormattedExpenditureDate ...

但保留ORDER BY子句的列名。