Home > Backend Development > PHP Tutorial > 求助下,mysql按字符串型日期排序如何做?

求助下,mysql按字符串型日期排序如何做?

WBOY
Release: 2016-06-23 14:08:36
Original
1635 people have browsed it

表中日期使用的是varchar型日期数据,值(如:20130301,20130201,20130101...20121201,20121101...)
想实现的功能是想根据年月来选出日期最近的月份值,可不可以在sql语句中实现呢?(php+mysql)

求高手指点,在线等!


回复讨论(解决方案)

SELECT TIMESTAMPDIFF(MONTH,'20130301',now())

感谢版主xuzuning!但是我测试了一下,好像不对呢?截图中是我的表中的数据,和我用你的语句返回的结果。
排序也需要考虑年份的。再帮帮我吧,多谢了

我只是举个例子,如何使用,要卡你的场景
SELECT $ from tbl_name order by TIMESTAMPDIFF(YEAR,fpdm,now()), TIMESTAMPDIFF(MONTH,fpdm,now())

不会连 tbl_name 表示什么也不知道吧

呵呵,我是小白,十分感谢,嗯 ,这个我还能知道啥意思,多谢版主!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template