2013-05-12 140 views
3

我有一個查詢,我試圖按最上面的日期排序,最下面排在最前面的結果。這是查詢按日期排序MySQL查詢排序只有一天,而不是全日期

SELECT headline, story, DATE_FORMAT(date, "%d-%M-%Y") AS date, 
name, logo, tpf_parks.park_id, url, alt, description 
FROM tpf_news 
INNER JOIN tpf_parks ON tpf_news.park_id = tpf_parks.park_id 
LEFT JOIN tpf_images ON tpf_news.news_id = tpf_images.news_id 
ORDER BY date DESC 

結果是有序的錯誤,雖然,它僅由這樣的結果是有序的這樣子日子排序是:25-march-2013, 23-april-2013, 19-febuary-2013, 10-may-2013等等。我認爲它,因爲這樣我顯示日期DATE_FORMAT(date, "%d-%M-%Y") AS date,但我不知道如何解決它。有任何想法嗎?

回答

8

試試這個:

SELECT headline, story, DATE_FORMAT(date, "%d-%M-%Y") AS Displaydate, 
    name, logo, tpf_parks.park_id, url, alt, description 
    FROM tpf_news 
    INNER JOIN tpf_parks ON tpf_news.park_id = tpf_parks.park_id 
    LEFT JOIN tpf_images ON tpf_news.news_id = tpf_images.news_id 
    ORDER BY date DESC 

這樣,MySQL將被真正的日期字段順序,以及應用程序代碼會顯示日期格式設置的方式,你希望它

3

重命名你的別名,它是曖昧

SELECT headline, story, DATE_FORMAT(date, "%d-%M-%Y") AS formatted_date, 
name, logo, tpf_parks.park_id, url, alt, description 
FROM tpf_news 
INNER JOIN tpf_parks ON tpf_news.park_id = tpf_parks.park_id 
LEFT JOIN tpf_images ON tpf_news.news_id = tpf_images.news_id 
ORDER BY date DESC 

然後數據庫引擎可以使用date訂購併知道差到原來的列和格式之一。