位置: 编程技术 - 正文
推荐整理分享mysql常用日期时间/数值函数详解(必看)(mysql日期用什么数据类型),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:mysql日期用什么数据类型,mysql日期yyyymmdd,mysql的时间日期字段类型,mysql日期yyyymmdd,mysql日期用什么数据类型,mysql的日期,mysql常用日期时间类型及表示的数据范围,mysql的日期,内容如对您有帮助,希望把文章链接给更多的朋友!
1.日期时间函数
时间转化秒函数:time_to_sec
MySQL> select time_to_sec('::');+-------------------------+| time_to_sec('::') |+-------------------------+| | +-------------------------+1 row in set (0. sec)
秒转化时间函数:sec_to_time
mysql> select sec_to_time();+-------------------+| sec_to_time() |+-------------------+| :: | +-------------------+1 row in set (0. sec)
日期转为天数函数:to_days
mysql> select to_days('--');+-----------------------+| to_days('--') |+-----------------------+| NULL | +-----------------------+1 row in set, 1 warning (0. sec)
mysql> select to_days('--');+-----------------------+| to_days('--') |+-----------------------+| | +-----------------------+1 row in set (0. sec)
天数转化日期函数:from_days
mysql> select from_days(0);+--------------+| from_days(0) |+--------------+| -- | +--------------+1 row in set (0. sec)
mysql> select from_days();+----------------+| from_days() |+----------------+| -- | +----------------+1 row in set (0. sec)
字符串转换为日期函数:str_to_date
mysql> select str_to_date('-- ::','%Y-%m-%d %H:%i:%s');+--------------------------------------------------------+| str_to_date('-- ::','%Y-%m-%d %H:%i:%s') |+--------------------------------------------------------+| -- :: | +--------------------------------------------------------+1 row in set (0. sec)
日期转换为字符串函数:date_format
mysql> select date_format('-- ::','%Y%m%d %H%i%s');+----------------------------------------------------+| date_format('-- ::','%Y%m%d %H%i%s') |+----------------------------------------------------+| | +----------------------------------------------------+1 row in set (0. sec)
时间转换为字符串函数:time_format
mysql> select time_format('::','%H%i%s');+----------------------------------+| time_format('::','%H%i%s') |+----------------------------------+| | +----------------------------------+1 row in set (0. sec)
说明:
日期时间格式参数如下:
%M 月名字(January……December) %W 星期名字(Sunday……Saturday) %D 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。) %Y 年, 数字, 4 位 %y 年, 数字, 2 位 %a 缩写的星期名字(Sun……Sat) %d 月份中的天数, 数字(……) %e 月份中的天数, 数字(0……) %m 月, 数字(……) %c 月, 数字(1……) %b 缩写的月份名字(Jan……Dec) %j 一年中的天数(……) %H 小时(……) %k 小时(0……) %h 小时(……) %I 小时(……) %l 小时(1……) %i 分钟, 数字(……) %r 时间, 小时(hh:mm:ss [AP]M) %T 时间, 小时(hh:mm:ss) %S 秒(……) %s 秒(……) %p AM或PM %w 一个星期中的天数(0=Sunday ……6=Saturday ) %U 星期(0……), 这里星期天是星期的第一天 %u 星期(0……), 这里星期一是星期的第一天 %% 一个文字“%”。
提取表达式的日期部分
mysql> select date(now());+-------------+| date(now()) |+-------------+| -- | +-------------+1 row in set (0. sec)
返回表达式的星期索引(0=星期一,1=星期二, ……6= 星期天)。
mysql> select weekday(now());+----------------+| weekday(now()) |+----------------+| 3 | +----------------+1 row in set (0. sec)
返回表达式是一年的第几周
mysql> select week(now());+-------------+| week(now()) |+-------------+| | +-------------+1 row in set (0. sec)
WEEK()允许指定星期是否开始于星期天或星期一。如果第二个参数是0,星期从星期天开始,如果第二个参数是1, 从星期一开始,如下所示:
mysql> select week(now(),0);+---------------+| week(now(),0) |+---------------+| | +---------------+1 row in set (0. sec)
mysql> select week(now(),1);+---------------+| week(now(),1) |+---------------+| | +---------------+1 row in set (0. sec)
返回表达式一年中季度
mysql> select quarter(now());+----------------+| quarter(now()) |+----------------+| 2 | +----------------+1 row in set (0. sec)
返回表达式一周的第一天
mysql> select dayofweek(now());+------------------+| dayofweek(now()) |+------------------+| 5 | +------------------+1 row in set (0. sec)
返回表达式一个月的第几天
mysql> select dayofmonth(now());+-------------------+| dayofmonth(now()) |+-------------------+| | +-------------------+1 row in set (0. sec)
返回表达式一年的第几天
mysql> select dayofyear(now());+------------------+| dayofyear(now()) |+------------------+| | +------------------+1 row in set (0. sec)
返回表达式的星期名字
mysql> select dayname(now());+----------------+| dayname(now()) |+----------------+| Thursday | +----------------+1 row in set (0. sec)
返回表达式月份的名字
mysql> select monthname(now());+------------------+| monthname(now()) |+------------------+| May | +------------------+1 row in set (0. sec)
mysql>
提取表达式的年份
mysql> select year(now());+-------------+| year(now()) |+-------------+| | +-------------+1 row in set (0. sec)
提取表达式的月份
mysql> select month(now());+--------------+| month(now()) |+--------------+| 5 | +--------------+1 row in set (0. sec)
提取表达式的天数
mysql> select day(now());+------------+| day(now()) |+------------+| | +------------+1 row in set (0. sec)
提取表达式的小时
mysql> select hour(now());+-------------+| hour(now()) |+-------------+| | +-------------+1 row in set (0. sec)
提取表达式的分钟
mysql> select minute(now());+---------------+| minute(now()) |+---------------+| | +---------------+1 row in set (0. sec)
提取表达式的秒数
mysql> select second(now());+---------------+| second(now()) |+---------------+| | +---------------+1 row in set (0. sec)
将当前日期按照'YYYY-MM-DD' 或YYYYMMDD 格式的值返回,具体格式根据函数用在字符串或是数字语境中而定。
mysql> select curdate();+------------+| curdate() |+------------+| -- | +------------+1 row in set (0. sec)
mysql> select curdate()+1;+-------------+| curdate()+1 |+-------------+| | +-------------+1 row in set (0. sec)
将当前时间以'HH:MM:SS'或 HHMMSS的格式返回,具体格式根据函数用在字符串或是数字语境中而定。
mysql> select curtime();+-----------+| curtime() |+-----------+| :: | +-----------+1 row in set (0. sec)
mysql> select curtime()+1;+---------------+| curtime()+1 |+---------------+| . | +---------------+1 row in set (0. sec)
获取当前日期时间:sysdate(),now()
mysql> select sysdate(),sleep(2),sysdate();+---------------------+----------+---------------------+| sysdate() | sleep(2) | sysdate() |+---------------------+----------+---------------------+| -- :: | 0 | -- :: | +---------------------+----------+---------------------+1 row in set (2. sec)
mysql> select now(),sleep(2),now();+---------------------+----------+---------------------+| now() | sleep(2) | now() |+---------------------+----------+---------------------+| -- :: | 0 | -- :: | +---------------------+----------+---------------------+1 row in set (2. sec)从上面可以看到sysdate和now的区别,now表示语句开始的时间,而sysdate实时的获取时间
将当前日期按照'YYYY-MM-DD HH:MM:SS'或YYYYMMDDHHMMSS格式的值返回,具体格式根据函数用在字符串或是数字语境中而定。
mysql> select current_timestamp;+---------------------+| current_timestamp |+---------------------+| -- :: | +---------------------+1 row in set (0. sec)
mysql> select current_timestamp+1;+-----------------------+| current_timestamp+1 |+-----------------------+| . | +-----------------------+1 row in set (0. sec)
unix_timestamp(),unix_timestamp(date)
如果没有参数调用,返回一个Unix时间戳记(从'-- ::'GMT开始的秒数)。如果UNIX_TIMESTAMP()用一 个date参数被调用,它返回从'-- ::' GMT开始的秒数值。date可以是一个DATE字符串、一个DATETIME 字符串、一个TIMESTAMP或以YYMMDD或YYYYMMDD格式的本地时间的一个数字。 mysql> select unix_timestamp();+------------------+| unix_timestamp() |+------------------+| | +------------------+1 row in set (0. sec)
mysql> select unix_timestamp('-- ::');+---------------------------------------+| unix_timestamp('-- ::') |+---------------------------------------+| | +---------------------------------------+1 row in set (0. sec)
mysql>
FROM_UNIXTIME(unix_timestamp)
以'YYYY-MM-DD HH:MM:SS'或YYYYMMDDHHMMSS格式返回unix_timestamp参数所表示的值,具体格式根据函数用在字符串或是数字语境中而定
mysql> select from_unixtime();+---------------------------+| from_unixtime() |+---------------------------+| -- :: | +---------------------------+1 row in set (0. sec)
mysql> select from_unixtime()+1;+-----------------------------+| from_unixtime()+1 |+-----------------------------+| . | +-----------------------------+1 row in set (0. sec)
mysql> select from_unixtime(,'%Y-%m-%d %h:%i:%s');+-----------------------------------------------+| from_unixtime(,'%Y-%m-%d %h:%i:%s') |+-----------------------------------------------+| -- :: | +-----------------------------------------------+1 row in set (0. sec)
返回表达式所在月的最后一天
mysql> select last_day(now());+-----------------+| last_day(now()) |+-----------------+| -- | +-----------------+1 row in set (0. sec)
日期加减运算
DATE_ADD(date,INTERVAL expr type) --加法DATE_SUB(date,INTERVAL expr type) --减法
mysql> select date_add('-- ::',interval 1 second);+---------------------------------------------------+| date_add('-- ::',interval 1 second) |+---------------------------------------------------+| -- :: | +---------------------------------------------------+1 row in set (0. sec)
mysql> select date_add('-- ::',interval 1 day);+------------------------------------------------+| date_add('-- ::',interval 1 day) |+------------------------------------------------+| -- :: | +------------------------------------------------+1 row in set (0. sec)
mysql> select date_add('-- ::',interval 1 minute);+---------------------------------------------------+| date_add('-- ::',interval 1 minute) |+---------------------------------------------------+| -- :: | +---------------------------------------------------+1 row in set (0. sec)
mysql> select date_add('-- ::',interval 1 hour);+-------------------------------------------------+| date_add('-- ::',interval 1 hour) |+-------------------------------------------------+| -- :: | +-------------------------------------------------+1 row in set (0. sec)
mysql> select date_add('-- ::',interval '1:1' minute_second);+--------------------------------------------------------------+| date_add('-- ::',interval '1:1' minute_second) |+--------------------------------------------------------------+| -- :: | +--------------------------------------------------------------+1 row in set (0. sec)
mysql> select date_add('-- ::', interval '1 1:1:1' day_second);+----------------------------------------------------------------+| date_add('-- ::', interval '1 1:1:1' day_second) |+----------------------------------------------------------------+| -- :: | +----------------------------------------------------------------+1 row in set (0. sec)
type值格式:
SECOND 秒 SECONDS MINUTE 分钟 MINUTES HOUR 时间 HOURS DAY 天 DAYS MONTH 月 MONTHS YEAR 年 YEARS MINUTE_SECOND 分钟和秒 "MINUTES:SECONDS" HOUR_MINUTE 小时和分钟 "HOURS:MINUTES" DAY_HOUR 天和小时 "DAYS HOURS" YEAR_MONTH 年和月 "YEARS-MONTHS" HOUR_SECOND 小时, 分钟, "HOURS:MINUTES:SECONDS" DAY_MINUTE 天, 小时, 分钟 "DAYS HOURS:MINUTES" DAY_SECOND 天, 小时, 分钟, 秒 "DAYS HOURS:MINUTES:SECONDS"
DATEDIFF(expr, expr2)
返回起始时间 expr和结束时间expr2之间的天数。Expr和expr2为日期或 date-and-time 表达式。计算中只用到这些值的日期部分。
mysql> select datediff('-- ::', '-- ::');+--------------------------------------------------------+| datediff('-- ::', '-- ::') |+--------------------------------------------------------+| -1 | +--------------------------------------------------------+1 row in set (0. sec)
表示日期时间的数据类型:
datetimeyeardatetimetimestamp
在使用日期时间数据比较时常用如下
mysql> select * from tab ;+------+---------------------+| name | createtime |+------+---------------------+| aaaa | -- :: || bbbb | -- :: || bbbb | -- :: || bbbb | -- :: |+------+---------------------+4 rows in set (0. sec)
mysql> select now();+---------------------+| now() |+---------------------+| -- :: |+---------------------+1 row in set (0. sec)
mysql> select * from tab where createtime > now();+------+---------------------+| name | createtime |+------+---------------------+| aaaa | -- :: |+------+---------------------+1 row in set (0. sec)
mysql> select current_timestamp;+---------------------+| current_timestamp |+---------------------+| -- :: |+---------------------+1 row in set (0. sec)
mysql> select * from tab where createtime > current_timestamp;+------+---------------------+| name | createtime |+------+---------------------+| aaaa | -- :: |+------+---------------------+1 row in set (0. sec)
mysql> select * from tab where createtime> str_to_date('-- ::','%Y-%m-%d %H:%i:%s');;+------+---------------------+| name | createtime |+------+---------------------+| aaaa | -- :: |+------+---------------------+1 row in set (0. sec)
mysql> select * from tab where createtime between str_to_date('-- ::','%Y-%m-%d %H:%i:%s') and str_to_date('-- ::','%Y-%m-%d %H:%i:%s')-> ;+------+---------------------+| name | createtime |+------+---------------------+| aaaa | -- :: |+------+---------------------+1 row in set (0. sec)
mysql> select * from tab where createtime between '-- ::' and '-- ::';+------+---------------------+| name | createtime |+------+---------------------+| aaaa | -- :: |+------+---------------------+1 row in set (0. sec)
mysql>
2.数值函数
ABS(X) :返回表达式X的绝对值
mysql> select abs(-2);+---------+| abs(-2) |+---------+| 2 | +---------+1 row in set (0. sec)
FLOOR(X) :返回不大于X的最大整数值
mysql> select floor(-2.);+--------------+| floor(-2.) |+--------------+| -3 | +--------------+1 row in set (0. sec)
MOD(N,M):模操作,返回N被M除后的余数。
mysql> select mod(3,2);+----------+| mod(3,2) |+----------+| 1 | +----------+1 row in set (0. sec)
RAND()/RAND(N) :返回一个随机浮点值数a,范围在 0 到1 之间 (即, 其范围为 0 ≤ a ≤ 1.0)。若已指定一个整数参数 N,则它被用作种子值,用来产生重复序列。
mysql> select rand();+-------------------+| rand() |+-------------------+| 0. | +-------------------+1 row in set (0. sec)
mysql> select rand(2);+-------------------+| rand(2) |+-------------------+| 0. | +-------------------+1 row in set (0. sec)
ROUND(X)/ROUND(X,D) :返回参数X, 其值接近于最近似的整数。在有两个参数的情况下,返回 X ,其值保留到小数点后D位,而第D位的保留方式为四舍五入。若要接保留X值小数点左边的D位,可将D设为负值。
mysql> select round(2.4 );+-------------+| round(2.4 ) |+-------------+| 2 | +-------------+1 row in set (0. sec)
mysql> select round(2.,2 );+-----------------+| round(2.,2 ) |+-----------------+| 2. | +-----------------+1 row in set (0. sec)
mysql> select round(.,-1 );+-------------------+| round(.,-1 ) |+-------------------+| | +-------------------+1 row in set (0. sec)
标签: mysql日期用什么数据类型
本文链接地址:https://www.jiuchutong.com/biancheng/347590.html 转载请保留说明!友情链接: 武汉网站建设