我有一个查询,我正在尝试按最近的日期排在最前面,直到最低的最低结果。这是查询
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
,但我不知道如何解决它。有什么想法吗?
答案 0 :(得分: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将按真实日期字段排序,您的应用程序代码将按照您希望的方式显示日期
答案 1 :(得分: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
进行排序,并知道与原始列和格式化列的区别。