mysql to_days,str_to_date函数的使用

来源:互联网 发布:yum安装lnmp环境搭建 编辑:程序博客网 时间:2024/05/18 01:49
如果你操作数据库时想通过时间加以限制,那么请以这样的形式存储时间:year-month-day hour:minute:second,给一个linux下的存储方法:void  *gettime(char name[])  {struct tm *p;//char name[512];char c[5];        time_t t;        t=time(NULL);        p=localtime(&t);        sprintf(c,"%d",1900+p->tm_year);        strcat(name,c);        strcat(name,"-");        if(1+p->tm_mon<10)        strcat(name,"0");        sprintf(c,"%d",1+p->tm_mon);        strcat(name,c);        strcat(name,"-");        if(p->tm_mday<10)        strcat(name,"0");        sprintf(c,"%d",p->tm_mday);        strcat(name,c);        strcat(name," ");        if (p->tm_hour<10)        strcat(name,"0");        sprintf(c,"%d",p->tm_hour);        strcat (name,c);        strcat(name,":");        if(p->tm_min<10)        strcat(name,"0");        sprintf(c,"%d",p->tm_min);        strcat(name,c);        strcat(name,":");        if(p->tm_sec<10)        strcat(name ,"0");       sprintf(c,"%d",p->tm_sec);       strcat(name,c);      printf("current time is:%s\n",name);}时间被转换成了字符串,然后存储到数据库里,之后如果想查某个时间之前的,或者某个时间之后的,或者某个时间区间,那么就要再次将字符串转换成时间,两个函数to_days,str_to_date。(1)to_days就像它的名字一样,它只能转换到每一天,就是说一天的时间字符串会被转换成一个数,如mysql> select to_days('2010-11-22 14:39:51'); +--------------------------------+| to_days('2010-11-22 14:39:51') | +--------------------------------+ | 734463 | +--------------------------------+mysql> select to_days('2010-11-23 14:39:51'); +--------------------------------+ | to_days('2010-11-23 14:39:51') | +--------------------------------+ | 734464 | +--------------------------------+可以看出22日与23日的差别就是,转换之后的数增加了1,这个粒度的查询是比较粗糙的,可能不能满足我们的查询要求,那么就引入细粒度的查询方法str_to_date。(2)str_to_date这个函数可以把字符串时间完全的翻译过来,就很好用了。mysql> select str_to_date("2010-11-23 14:39:51",'%Y-%m-%d %H:%i:%s');+--------------------------------------------------------+| str_to_date("2010-11-23 14:39:51",'%Y-%m-%d %H:%i:%s') |+--------------------------------------------------------+| 2010-11-23 14:39:51                                    |+--------------------------------------------------------+我针对自己的数据库的一个查询操作select str_to_date(detectResult.`rcvDetectTime`,'%Y-%m-%d %H:%i:%s') from detectResult where str_to_date(detectResult.`rcvDetectTime`,'%Y-%m-%d %H:%i:%s')>='2010-11-22 14:49:52' and str_to_date(detectResult.`rcvDetectTime`,'%Y-%m-%d %H:%i:%s')<='2010-11-22 15:27:52'看一下结果吧+---------------------------------------------------------------+ | str_to_date(detectResult.`rcvDetectTime`,'%Y-%m-%d %H:%i:%s') | +---------------------------------------------------------------+ | 2010-11-22 14:50:31 | | 2010-11-22 14:51:51 | | 2010-11-22 14:53:11 | | 2010-11-22 14:54:31 | | 2010-11-22 14:55:51 | | 2010-11-22 14:57:11 | | 2010-11-22 14:58:31 | | 2010-11-22 14:59:51 | | 2010-11-22 15:01:11 | | 2010-11-22 15:02:31 | | 2010-11-22 15:03:51 | | 2010-11-22 15:05:11 | | 2010-11-22 15:06:31 | | 2010-11-22 15:07:51 | | 2010-11-22 15:09:11 | | 2010-11-22 15:10:31 | | 2010-11-22 15:11:51 | | 2010-11-22 15:13:12 | | 2010-11-22 15:14:32 | | 2010-11-22 15:15:52 | | 2010-11-22 15:17:12 | | 2010-11-22 15:18:32 | | 2010-11-22 15:19:52 | | 2010-11-22 15:21:12 | | 2010-11-22 15:22:32 | | 2010-11-22 15:23:52 | | 2010-11-22 15:25:12 | | 2010-11-22 15:26:32 | | 2010-11-22 15:27:52 | +---------------------------------------------------------------+