zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

mysql 数据库to_days,str_to_date函数的使用

mysql数据库 函数 to Date Str 使用
2023-09-27 14:23:03 时间
如果你操作数据库时想通过时间加以限制,那么请以这样的形式存储时间: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 detectResultwhere 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 |+---------------------------------------------------------------+
这样就可以按照时间进行查询了,对于实验结果的分析方便了很多。
mysql日期和字符相互转换方法
date_format(date,'%Y-%m-%d')      -------------->oracle中的to_char();
str_to_date(date,'%Y-%m-%d')       -------------->oracle中的to_date();

%Y:代表4位的年份
%y:代表2为的年份
 
%m:代表月, 格式为(01……12)  
%c:代表月, 格式为(1……12)
 
%d:代表月份中的天数,格式为(00……31)  
%e:代表月份中的天数, 格式为(0……31) 
 
%H:代表小时,格式为(00……23)  
%k:代表 小时,格式为(0……23)  
%h: 代表小时,格式为(01……12)  
%I: 代表小时,格式为(01……12)  
%l :代表小时,格式为(1……12)
   
%i: 代表分钟, 格式为(00……59) 

%r:代表 时间,格式为12 小时(hh:mm:ss [AP]M)  
%T:代表 时间,格式为24 小时(hh:mm:ss) 

%S:代表 秒,格式为(00……59)  
%s:代表 秒,格式为(00……59)