在mysql查询ORDER BY中更改时间格式 [英] change time format in mysql query ORDER BY
本文介绍了在mysql查询ORDER BY中更改时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在处理一个项目,其中用户具有以正常格式存储的日期的数据库 2013年3月8日,我想按日期对输出进行排序..但没有得到正确的输出,因为未使用bcos时间戳. ...所以有办法我可以使用该日期字段或将其转换为mysql查询中的时间戳
i am working on project where user have database with date stored in normal format 8 March 2013 and i want to sort the output with date .. but not getting right output bcos time stamp not used .... so is there way i can use or convert that date field to time stamp inside mysql query
SELECT * FROM imdb WHERE year ='2012' ORDER BY `date` DESC LIMIT 20
我想要这样的东西
SELECT * FROM imdb WHERE year ='2012' ORDER BY strtotime(`date`) DESC LIMIT 20
但是我知道这行不通...有没有出路
but i know this wont work ... is there a way out
推荐答案
由于不是日期,请使用STR_TO_DATE
将字符串转换为日期
since it is not a date, use STR_TO_DATE
to convert string to date
SELECT *
FROM imdb
WHERE year ='2012'
ORDER BY STR_TO_DATE(`date`, '%e %M %Y') DESC
LIMIT 20
- SQLFiddle演示
- SQLFiddle Demo
这篇关于在mysql查询ORDER BY中更改时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文