2012-05-29 80 views
0

我有一個不可避免的情況下日期將被保存在英國的日期格式,例如:Mysql的排序日期格式

31/12/2001 00:00:00 

我需要按降序排列,我已經試過,但它的錯誤

select *, DATE_FORMAT(completiondate,'\%e/%c/%Y\') as cdate 
from projects 
where countries = 1 
order by cdate desc 

錯誤:

check the manual that corresponds to your MySQL server version for the the right syntax to use near '' order by cdate desc'

我使用MySQL 4.1.9

+0

它沒有'order by'子句嗎? –

回答

1

你正在逃避%字符不必要的。但實際的問題是,你有一個未終止字符串您的查詢文字:

-- this does not terminate the string ----------v 
select *, DATE_FORMAT(completiondate,'\%e/%c/%Y\') as cdate 
from projects 
where countries = 1 
order by cdate desc 

更改爲:

SELECT *, DATE_FORMAT(completiondate,'%e/%c/%Y') AS cdate 
FROM projects 
WHERE countries = 1 
ORDER BY cdate DESC 
+0

斜槓導致的錯誤謝謝 –

0
SELECT * FROM projects WHERE countries = 1 order by cdate desc 

我猜你已經在你的數據庫cdate?如果是這樣,你不需要設置「date_format」,因爲它已經在那裏。 但我可能是錯誤的,因爲我從來沒有用過4.1.9

+1

OP需要轉換日期,因爲列是(var)char而不是日期時間 –

+0

這就是它的插孔謝謝 –

2

這是最終解決方案

select *,completiondate from projects order by str_to_date(completiondate,'%d/%m/%Y %H:%i') desc 
1

吉姆,您的最終解決方案對我來說是巨大的幫助。我的日期格式爲02/28/2013。我使用的代碼:

SELECT *,str_to_date(SaleDate,'%m/%d/%Y') AS cdate FROM mytable ORDER BY cdate DESC 

謝謝!