Home >Database >Mysql Tutorial >mysql数据库取得某一段时间内的纪录_MySQL

mysql数据库取得某一段时间内的纪录_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-01 13:34:491414browse

bitsCN.com

mysql数据库取得某一段时间内的纪录

 

一般来说,我们在mysql数据库纪录数据时间时,都会选择datatime类型,这样时间可以精确到秒。但随之而来的一个问题是,当我们要取得某一段时间内的数据内容会有一些时间转换上的麻烦,例如我们要取得2002年3月2日到2003年7月8日的这段时间的内容,我们第一个直觉就是应该将sql写成下面的形式:

 

select * from yourdb where sj>='2002-3-2' and sj

 

 这样以来,我们取得数据内容就是2002年3月2日0点以后到2003年7月8日0点(也就是7月7号12点之前)之间的内容,这个当然不是我们想要的,所以需要使用mysql的时间函数to_days()来写这个sql:

 

select * from yourdb where to_days(sj)>=to_days('2002-3-2') and sj

 

 现在,我们所取得就是我们想要的结果了。

 

比如我要取这样的广告位 :广告位id为1,并且时间上前后限定;

 

select * from t_ads a where a.adsPosId = 1 and TO_DAYS(NOW()) >= TO_DAYS(a.beginDate)

and TO_DAYS(NOW()) bitsCN.com

Statement:
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