3

我有一个查询,我试图按顶部的最近日期排序,向下到底部最旧的结果。这是查询

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,但我不知道如何修复它。有任何想法吗?

4

2 回答 2

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 将按真实日期字段排序,您的应用程序代码将显示您想要的日期格式

于 2013-05-12T14:41:14.667 回答
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排序并知道原始列和格式化列的区别。

于 2013-05-12T14:41:16.923 回答