MySql 日期函数
在 MySql 中经常会用到日期,关于常用的日期函数,做了以下的总结:
1 . now()
作用; 获取当前的日期
除此之外,获取当前日期的函数还有: current_timestamp(); current_time; localtime(); localtime; localtimestamp(); localtimestamp;但是这些日期函数,与 now() 的效果相同, 为了方便记忆,建议使用 now() 来代替上面的函数。
- mysql> select now();
- +---------------------+
- | now() |
- +---------------------+
- | -- :: |
- +---------------------+
- row in set
- mysql> select datediff(now(),'2015-1-1');
- +----------------------------+
- | datediff(now(),'2015-1-1') |
- +----------------------------+
- | |
- +----------------------------+
- row in set
- mysql> select localtime();
- +---------------------+
- | localtime() |
- +---------------------+
- | -- :: |
- +---------------------+
- row in set
- mysql> select current_timestamp();
- +---------------------+
- | current_timestamp() |
- +---------------------+
- | -- :: |
- +---------------------+
- row in set
- mysql> select localtime;
- +---------------------+
- | localtime |
- +---------------------+
- | -- :: |
- +---------------------+
- row in set
- mysql> select localtimestamp;
- +---------------------+
- | localtimestamp |
- +---------------------+
- | -- :: |
- +---------------------+
- row in set
- mysql> select localtimestamp();
- +---------------------+
- | localtimestamp() |
- +---------------------+
- | -- :: |
- +---------------------+
- row in set
- mysql> select sysdate();
- +---------------------+
- | sysdate() |
- +---------------------+
- | -- :: |
- +---------------------+
- row in set
2. sysdate()
作用: 这个日期函数与 now() 类似,不同的地方是: now() 在执行开始时就得到的确定,而 sysdate() 则是在运行时动态得到 值。
如下所示:
- mysql> select now(), sleep(30), now();
- +---------------------+-----------+---------------------+
- | now() | sleep(30) | now() |
- +---------------------+-----------+---------------------+
- | 2015-05-13 15:01:49 | 0 | 2015-05-13 15:01:49 |
- +---------------------+-----------+---------------------+
- 1 row in set
- mysql> select sysdate(), sleep(30), sysdate();
- +---------------------+-----------+---------------------+
- | sysdate() | sleep(30) | sysdate() |
- +---------------------+-----------+---------------------+
- | 2015-05-13 15:02:52 | 0 | 2015-05-13 15:03:22 |
- +---------------------+-----------+---------------------+
- 1 row in set
在用 now() 时,在途中 sleep 了 30秒 ,但是 Now() 的结果是相同的, 而在 sysdate() 中 sleep 了 30秒, 结果就不同了,结果是 sleep 30 秒后的值
3. curdate()
作用; 获取当前日期
函数 current_date(); current_date; 与 curdate() 功能一样
- mysql> select curdate();
- +------------+
- | curdate() |
- +------------+
- | 2015-05-13 |
- +------------+
- 1 row in set
- mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2015-05-13 |
+----------------+
1 row in set- mysql> select current_date;
+--------------+
| current_date |
+--------------+
| 2015-05-13 |
+--------------+
1 row in set
4. curtime()
作用: 获取当前时间 ,其功能与 current_time current_time() 是一样的
- mysql> select curtime();
- +-----------+
- | curtime() |
- +-----------+
- | 15:04:42 |
- +-----------+
- 1 row in set
- mysql> select current_time();
- +----------------+
- | current_time() |
- +----------------+
- | 15:05:05 |
- +----------------+
- 1 row in set
- mysql> select current_time;
- +--------------+
- | current_time |
- +--------------+
- | 15:05:09 |
- +--------------+
- 1 row in set
5. utc_date(); utc_time(); utc_timestamp();
作用: 获取当前 utc 时间的函数
- mysql> select utc_timestamp(), utc_date(),utc_time(),now();
- +---------------------+------------+------------+---------------------+
- | utc_timestamp() | utc_date() | utc_time() | now() |
- +---------------------+------------+------------+---------------------+
- | 2015-05-13 07:05:51 | 2015-05-13 | 07:05:51 | 2015-05-13 15:05:51 |
- +---------------------+------------+------------+---------------------+
- 1 row in set
6. date(); year(); month(); day(); time(); week(); hour(); minute(); second(); microsecond();
作用; 获取日期中的部分值
- mysql> set @date='2015-05-05 14:23:34.345687'
- -> select date(@date)
- -> select time(@date)
- -> select year(@date)
- -> select quarter(@date)
- -> select month(@date)
- -> select week(@date)
- -> select day(@date)
- -> select hour(@date)
- -> select minute(@date)
- -> select second(@date)
- -> select microsecond(@date);
7. extract()
作用: 和上面所列举的函数功能一样, 只是函数的书写方式不同
- mysql> set @date='2015-05-05 14:23:34.345687'
- -> select extract(year from @date)
8. last_day()
作用: 返回月份的最后一天
9. datedifff()
作用: 计算两个日期间的相差的天数
- mysql> select datediff(now(),'2015-05-05');
- +------------------------------+
- | datediff(now(),'2015-05-05') |
- +------------------------------+
- | 8 |
- +------------------------------+
10. str_to_date(str,format)
作用: 可以把一些杂乱无章的字符串转换成日期格式,也可以转换成时间
有关更多的 Mysql 函数,可以参考 Mysql 在线手册:http://www.cbi.pku.edu.cn/chinese/documents/csdoc/mysql/manual_toc.html
MySql 日期函数的更多相关文章
- [php基础]Mysql日期函数:日期时间格式转换函数详解
在PHP网站开发中,Mysql数据库设计中日期时间字段必不可少,由于Mysql日期函数输出的日期格式与PHP日期函数之间的日期格式兼容性不够,这就需要根据网站实际情况使用Mysql或PHP日期转换函数 ...
- mysql 日期函数总结
1.0 格式化:DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据. 语法 DATE_FORMAT(date,format) date 参数是合法的日期.format 规定日期/时间的 ...
- MYSQL 日期函数【转】
MySQL日期时间函数大全 DAYOFWEEK(date) 返回日期date是星期几(=星期六,ODBC标准) mysql> select DAYOFWEEK('1998-02-03'); WE ...
- mysql日期函数(转)
MySQL 获得当前日期时间 函数 获得当前日期+时间(date + time)函数:now() mysql> select now(); +---------------------+ | n ...
- Mysql日期函数,时间函数使用的总结
一.MySQL 获得当前日期时间 函数 1.1 获得当前日期+时间(date + time)函数:now() mysql> select now();+--------------------- ...
- MySQL:日期函数、时间函数总结
MySQL 获得当前日期时间 函数 查询昨天,时间拼接 select concat(DATE_FORMAT(date_add(now(), interval -1 day),'%Y-%d-%d'),& ...
- MySQL日期函数、时间函数总结(MySQL 5.X)
一.获得当前日期时间函数 1.1 获得当前日期+时间(date + time)函数:now() select now(); # :: 除了 now() 函数能获得当前的日期时间外,MySQL 中还有下 ...
- mysql 日期函数大全
对于每个类型拥有的值范围以及并且指定日期何时间值的有效格式的描述见7.3.6 日期和时间类型. 这里是一个使用日期函数的例子.下面的查询选择了所有记录,其date_col的值是在最后30天以内: my ...
- MySQL日期函数与日期转换格式化函数大全
Mysql作为一款开元的免费关系型数据库,用户基础非常庞大,本文列出了MYSQL常用日期函数与日期转换格式化函数 1.DAYOFWEEK(date) 1 2 SELECT DAYOFWEEK('201 ...
随机推荐
- linux 鼠标中键粘帖功能?!!
转载自:http://yjhexy.iteye.com/blog/785564 ubuntu鼠标中键问题,其实也不是什么问题,ubuntu的鼠标中键是用来快速粘贴的,只是windows用惯了,时不时手 ...
- 一. Logback与p6spy
一. LogBack配置 配置pom.xml <dependency> <groupId>org.slf4j</groupId> <artifactId> ...
- CF 369C . Valera and Elections tree dfs 好题
C. Valera and Elections The city Valera lives in is going to hold elections to the city Parliament ...
- python(6)时间戳和北京时间互转,输出当前的时间和推到七天前的日期
项目发展的需要:(包含时间函数)time datetime 时间戳和北京时间互转 import time import datetime s = '2015-04-17 11:25:30' d = d ...
- Java System.out的输出缓冲
今天学习了java的正则表达式api,在写例子的时候遇到了让人摸不着头脑的问题:从控制台输入了字符串,却没有输出;直到输入的字符串不能匹配的时,一起与Unabled to match输出.相关代码如下 ...
- nginx优化配置
一.一般来说nginx 配置文件中对优化比较有作用的为以下几项: 1. worker_processes 8; nginx 进程数,建议按照cpu 数目来指定,一般为它的倍数 (如,2个四核的cpu ...
- Mac 10.10下安装MySQL5.6.21提示安装失败
只要要在安装的第三步在自定里不要选Startup item就可以了
- Chrome每次打開都要打開123.sogou.com
剛開始還以為中毒了,又是殺毒又是掃描的,最後發覺,原來就是chrome的一個設置被改了. Chrome->設置->啟動時 : 選打开特定网页或一组网页->設置網頁 , 將其中的123 ...
- ndk的一些概念
什么场景应用ndk 1.代码的包含,apk的java层代码容易被反编译,c/c++被反编译难度非常大 2.NDK中调用 第三方C/C++库,因为大部分的开源库都是c/c++编写,比如opencv,op ...
- html5 基本内容 摘自W3C
HTML5 教程(摘录自 W3C School) HTML 5 简介(HTML5 是下一代的 HTML) 什么是 HTML5? HTML5 将成为 HTML.XHTML 以及 HTML DOM 的新标 ...