2017-08-08 158 views
0

我在mysql表中有一列hiredate。我不得不選擇這個列的數據類型作爲varchar,因爲有時候我將hiredate的值設爲'xxxx-xx-xx'或'zzzz-zz-zz',我需要將這些值保存在數據庫中。命令通過hiredate DESC不按預期在mysql中工作

我的問題是,當我與

select distinct 
    agentname 
from agentdata 
where hiredate not in ('xxxx-xx-xx','zzzz-zz-zz') 
order by hiredate desc 

條款執行查詢,它不返回正確排序數據。但是當我用

select distinct 
    agentname, 
    hiredate 
from agentdata 
where hiredate not in ('xxxx-xx-xx','zzzz-zz-zz') 
order by hiredate desc 

子句執行查詢時,它返回了正確的排序數據。

區別在於select語句中只有hiredate字段。我不知道這種Mysql行爲的原因。

+0

您向我們展示的兩個查詢是不同的,並且不返回相同的列。比較他們是有點蘋果橙色IMO。 –

+0

是的,這兩個是不同的,如果你想正確地對結果集進行排序,那麼在select子句中必須有列名?如果我只希望代理人名稱按職位排序,該怎麼辦? str_to_date(hiredate,'%Y-%m-%d')沒有爲我工作。 – Devendra

回答

1

您需要按日期排序並按日期排序。

ORDER BY str_to_date(hiredate, '%Y-%m-%d') 
+0

對不起,它沒有工作:( – Devendra