1.日期时间函数
时间转化秒函数:time_to_sec
MySQL> select time_to_sec('01:01:01');
+-------------------------+
| time_to_sec('01:01:01') |
+-------------------------+
| 3661 |
+-------------------------+
1 row in set (0.00 sec)
秒转化时间函数:sec_to_time
mysql> select sec_to_time(3661);
+-------------------+
| sec_to_time(3661) |
+-------------------+
| 01:01:01 |
+-------------------+
1 row in set (0.00 sec)
日期转为天数函数:to_days
mysql> select to_days('0000-00-00');
+-----------------------+
| to_days('0000-00-00') |
+-----------------------+
| NULL |
+-----------------------+
1 row in set, 1 warning (0.01 sec)
mysql> select to_days('0001-01-01');
+-----------------------+
| to_days('0001-01-01') |
+-----------------------+
| 366 |
+-----------------------+
1 row in set (0.00 sec)
天数转化日期函数:from_days
mysql> select from_days(0);
+--------------+
| from_days(0) |
+--------------+
| 0000-00-00 |
+--------------+
1 row in set (0.00 sec)
mysql> select from_days(366);
+----------------+
| from_days(366) |
+----------------+
| 0001-01-01 |
+----------------+
1 row in set (0.00 sec)
字符串转换为日期函数:str_to_date
mysql> select str_to_date('2013-01-01 01:21:01','%Y-%m-%d %H:%i:%s');
+--------------------------------------------------------+
| str_to_date('2013-01-01 01:21:01','%Y-%m-%d %H:%i:%s') |
+--------------------------------------------------------+
| 2013-01-01 01:21:01 |
+--------------------------------------------------------+
1 row in set (0.00 sec)
日期转换为字符串函数:date_format
mysql> select date_format('2013-01-01 01:21:01','%Y%m%d %H%i%s');
+----------------------------------------------------+
| date_format('2013-01-01 01:21:01','%Y%m%d %H%i%s') |
+----------------------------------------------------+
| 20130101 012101 |
+----------------------------------------------------+
1 row in set (0.00 sec)
时间转换为字符串函数:time_format
mysql> select time_format('01:21:01','%H%i%s');
+----------------------------------+
| time_format('01:21:01','%H%i%s') |
+----------------------------------+
| 012101 |
+----------------------------------+
1 row in set (0.00 sec)
说明:
日期时间格式参数如下:
%M 月名字(January……December)
%W 星期名字(Sunday……Saturday)
%D 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)
%Y 年, 数字, 4 位
%y 年, 数字, 2 位
%a 缩写的星期名字(Sun……Sat)
%d 月份中的天数, 数字(00……31)
%e 月份中的天数, 数字(0……31)
%m 月, 数字(01……12)
%c 月, 数字(1……12)
%b 缩写的月份名字(Jan……Dec)
%j 一年中的天数(001……366)
%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)
%p AM或PM
%w 一个星期中的天数(0=Sunday ……6=Saturday )
%U 星期(0……52), 这里星期天是星期的第一天
%u 星期(0……52), 这里星期一是星期的第一天
%% 一个文字“%”。
提取表达式的日期部分
mysql> select date(now());
+-------------+
| date(now()) |
+-------------+
| 2013-05-16 |
+-------------+
1 row in set (0.00 sec)
返回表达式的星期索引(0=星期一,1=星期二, ……6= 星期天)。
mysql> select weekday(now());
+----------------+
| weekday(now()) |
+----------------+
| 3 |
+----------------+
1 row in set (0.00 sec)
返回表达式是一年的第几周
mysql> select week(now());
+-------------+
| week(now()) |
+-------------+
| 19 |
+-------------+
1 row in set (0.00 sec)
WEEK()允许指定星期是否开始于星期天或星期一。如果第二个参数是0,星期从星期天开始,如果第二个参数是1, 从星期一开始,如下所示:
mysql> select week(now(),0);
+---------------+
| week(now(),0) |
+---------------+
| 19 |
+---------------+
1 row in set (0.00 sec)
mysql> select week(now(),1);
+---------------+
| week(now(),1) |
+---------------+
| 20 |
+---------------+
1 row in set (0.00 sec)
返回表达式一年中季度
mysql> select quarter(now());
+----------------+
| quarter(now()) |
+----------------+
| 2 |
+----------------+
1 row in set (0.00 sec)
返回表达式一周的第一天
mysql> select dayofweek(now());
+------------------+
| dayofweek(now()) |
+------------------+
| 5 |
+------------------+
1 row in set (0.00 sec)
返回表达式一个月的第几天
mysql> select dayofmonth(now());
+-------------------+
| dayofmonth(now()) |
+-------------------+
| 16 |
+-------------------+
1 row in set (0.00 sec)
返回表达式一年的第几天
mysql> select dayofyear(now());
+------------------+
| dayofyear(now()) |
+------------------+
| 136 |
+------------------+
1 row in set (0.00 sec)
返回表达式的星期名字
mysql> select dayname(now());
+----------------+
| dayname(now()) |
+----------------+
| Thursday |
+----------------+
1 row in set (0.00 sec)
返回表达式月份的名字
mysql> select monthname(now());
+------------------+
| monthname(now()) |
+------------------+
| May |
+------------------+
1 row in set (0.00 sec)
mysql>
提取表达式的年份
mysql> select year(now());
+-------------+
| year(now()) |
+-------------+
| 2013 |
+-------------+
1 row in set (0.00 sec)
提取表达式的月份
mysql> select month(now());
+--------------+
| month(now()) |
+--------------+
| 5 |
+--------------+
1 row in set (0.01 sec)
提取表达式的天数
mysql> select day(now());
+------------+
| day(now()) |
+------------+
| 16 |
+------------+
1 row in set (0.00 sec)
提取表达式的小时
mysql> select hour(now());
+-------------+
| hour(now()) |
+-------------+
| 16 |
+-------------+
1 row in set (0.00 sec)
提取表达式的分钟
mysql> select minute(now());
+---------------+
| minute(now()) |
+---------------+
| 31 |
+---------------+
1 row in set (0.00 sec)
提取表达式的秒数
mysql> select second(now());
+---------------+
| second(now()) |
+---------------+
| 34 |
+---------------+
1 row in set (0.00 sec)
将当前日期按照'YYYY-MM-DD' 或YYYYMMDD 格式的值返回,具体格式根据函数用在字符串或是数字语境中而定。
mysql> select curdate();
+------------+
| curdate() |
+------------+
| 2013-05-16 |
+------------+
1 row in set (0.00 sec)
mysql> select curdate()+1;
+-------------+
| curdate()+1 |
+-------------+
| 20130517 |
+-------------+
1 row in set (0.00 sec)
将当前时间以'HH:MM:SS'或 HHMMSS的格式返回,具体格式根据函数用在字符串或是数字语境中而定。
mysql> select curtime();
+-----------+
| curtime() |
+-----------+
| 16:43:10 |
+-----------+
1 row in set (0.00 sec)
mysql> select curtime()+1;
+---------------+
| curtime()+1 |
+---------------+
| 164420.000000 |
+---------------+
1 row in set (0.00 sec)
获取当前日期时间:sysdate(),now()
mysql> select sysdate(),sleep(2),sysdate();
+---------------------+----------+---------------------+
| sysdate() | sleep(2) | sysdate() |
+---------------------+----------+---------------------+
| 2013-05-16 17:16:04 | 0 | 2013-05-16 17:16:06 |
+---------------------+----------+---------------------+
1 row in set (2.00 sec)
mysql> select now(),sleep(2),now();
+---------------------+----------+---------------------+
| now() | sleep(2) | now() |
+---------------------+----------+---------------------+
| 2013-05-16 17:16:18 | 0 | 2013-05-16 17:16:18 |
+---------------------+----------+---------------------+
1 row in set (2.00 sec)
从上面可以看到sysdate和now的区别,now表示语句开始的时间,而sysdate实时的获取时间
将当前日期按照'YYYY-MM-DD HH:MM:SS'或YYYYMMDDHHMMSS格式的值返回,具体格式根据函数用在字符串或是数字语境中而定。
mysql> select current_timestamp;
+---------------------+
| current_timestamp |
+---------------------+
| 2013-05-16 17:19:51 |
+---------------------+
1 row in set (0.00 sec)
mysql> select current_timestamp+1;
+-----------------------+
| current_timestamp+1 |
+-----------------------+
| 20130516172008.000000 |
+-----------------------+
1 row in set (0.00 sec)
unix_timestamp(),unix_timestamp(date)
如果没有参数调用,返回一个Unix时间戳记(从'1970-01-01 00:00:00'GMT开始的秒数)。如果UNIX_TIMESTAMP()用一
个date参数被调用,它返回从'1970-01-01 00:00:00' GMT开始的秒数值。date可以是一个DATE字符串、一个DATETIME
字符串、一个TIMESTAMP或以YYMMDD或YYYYMMDD格式的本地时间的一个数字。
mysql> select unix_timestamp();
+------------------+
| unix_timestamp() |
+------------------+
| 1368696216 |
+------------------+
1 row in set (0.00 sec)
mysql> select unix_timestamp('2013-05-16 01:01:01');
+---------------------------------------+
| unix_timestamp('2013-05-16 01:01:01') |
+---------------------------------------+
| 1368637261 |
+---------------------------------------+
1 row in set (0.00 sec)
mysql>
FROM_UNIXTIME(unix_timestamp)
以'YYYY-MM-DD HH:MM:SS'或YYYYMMDDHHMMSS格式返回unix_timestamp参数所表示的值,具体格式根据函数用在字符串或是数字语境中而定
mysql> select from_unixtime(1368637261);
+---------------------------+
| from_unixtime(1368637261) |
+---------------------------+
| 2013-05-16 01:01:01 |
+---------------------------+
1 row in set (0.00 sec)
mysql> select from_unixtime(1368637261)+1;
+-----------------------------+
| from_unixtime(1368637261)+1 |
+-----------------------------+
| 20130516010102.000000 |
+-----------------------------+
1 row in set (0.00 sec)
mysql> select from_unixtime(1368637261,'%Y-%m-%d %h:%i:%s');
+-----------------------------------------------+
| from_unixtime(1368637261,'%Y-%m-%d %h:%i:%s') |
+-----------------------------------------------+
| 2013-05-16 01:01:01 |
+-----------------------------------------------+
1 row in set (0.00 sec)
返回表达式所在月的最后一天
mysql> select last_day(now());
+-----------------+
| last_day(now()) |
+-----------------+
| 2013-05-31 |
+-----------------+
1 row in set (0.00 sec)
日期加减运算
DATE_ADD(date,INTERVAL expr type) --加法
DATE_SUB(date,INTERVAL expr type) --减法
mysql> select date_add('2013-05-16 01:01:01',interval 1 second);
+---------------------------------------------------+
| date_add('2013-05-16 01:01:01',interval 1 second) |
+---------------------------------------------------+
| 2013-05-16 01:01:02 |
+---------------------------------------------------+
1 row in set (0.00 sec)
mysql> select date_add('2013-05-16 01:01:01',interval 1 day);
+------------------------------------------------+
| date_add('2013-05-16 01:01:01',interval 1 day) |
+------------------------------------------------+
| 2013-05-17 01:01:01 |
+------------------------------------------------+
1 row in set (0.00 sec)
mysql> select date_add('2013-05-16 01:01:01',interval 1 minute);
+---------------------------------------------------+
| date_add('2013-05-16 01:01:01',interval 1 minute) |
+---------------------------------------------------+
| 2013-05-16 01:02:01 |
+---------------------------------------------------+
1 row in set (0.00 sec)
mysql> select date_add('2013-05-16 01:01:01',interval 1 hour);
+-------------------------------------------------+
| date_add('2013-05-16 01:01:01',interval 1 hour) |
+-------------------------------------------------+
| 2013-05-16 02:01:01 |
+-------------------------------------------------+
1 row in set (0.00 sec)
mysql> select date_add('2013-05-16 01:01:01',interval '1:1' minute_second);
+--------------------------------------------------------------+
| date_add('2013-05-16 01:01:01',interval '1:1' minute_second) |
+--------------------------------------------------------------+
| 2013-05-16 01:02:02 |
+--------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select date_add('2013-05-16 01:01:01', interval '1 1:1:1' day_second);
+----------------------------------------------------------------+
| date_add('2013-05-16 01:01:01', interval '1 1:1:1' day_second) |
+----------------------------------------------------------------+
| 2013-05-17 02:02:02 |
+----------------------------------------------------------------+
1 row in set (0.00 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('2013-05-16 06:01:01', '2013-05-17 01:01:01');
+--------------------------------------------------------+
| datediff('2013-05-16 06:01:01', '2013-05-17 01:01:01') |
+--------------------------------------------------------+
| -1 |
+--------------------------------------------------------+
1 row in set (0.00 sec)
表示日期时间的数据类型:
date
time
year
datetime
timestamp
在使用日期时间数据比较时常用如下
mysql> select * from tab ;
+------+---------------------+
| name | createtime |
+------+---------------------+
| aaaa | 2013-05-14 17:20:19 |
| bbbb | 2013-04-14 17:20:36 |
| bbbb | 2013-04-13 17:20:36 |
| bbbb | 2013-04-15 17:20:36 |
+------+---------------------+
4 rows in set (0.00 sec)
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2013-05-14 17:10:26 |
+---------------------+
1 row in set (0.00 sec)
mysql> select * from tab where createtime > now();
+------+---------------------+
| name | createtime |
+------+---------------------+
| aaaa | 2013-05-14 17:20:19 |
+------+---------------------+
1 row in set (0.00 sec)
mysql> select current_timestamp;
+---------------------+
| current_timestamp |
+---------------------+
| 2013-05-14 17:10:49 |
+---------------------+
1 row in set (0.00 sec)
mysql> select * from tab where createtime > current_timestamp;
+------+---------------------+
| name | createtime |
+------+---------------------+
| aaaa | 2013-05-14 17:20:19 |
+------+---------------------+
1 row in set (0.00 sec)
mysql> select * from tab where createtime> str_to_date('2013-05-14 00:00:00','%Y-%m-%d %H:%i:%s');;
+------+---------------------+
| name | createtime |
+------+---------------------+
| aaaa | 2013-05-14 17:20:19 |
+------+---------------------+
1 row in set (0.00 sec)
mysql> select * from tab where createtime between str_to_date('2013-05-14 00:00:00','%Y-%m-%d %H:%i:%s') and str_to_date('2013-05-15 00:00:00','%Y-%m-%d %H:%i:%s')
-> ;
+------+---------------------+
| name | createtime |
+------+---------------------+
| aaaa | 2013-05-14 17:20:19 |
+------+---------------------+
1 row in set (0.00 sec)
mysql> select * from tab where createtime between '2013-05-14 00:00:00' and '2013-05-15 00:00:00';
+------+---------------------+
| name | createtime |
+------+---------------------+
| aaaa | 2013-05-14 17:20:19 |
+------+---------------------+
1 row in set (0.00 sec)
mysql>
2.数值函数
ABS(X) :返回表达式X的绝对值
mysql> select abs(-2);
+---------+
| abs(-2) |
+---------+
| 2 |
+---------+
1 row in set (0.00 sec)
FLOOR(X) :返回不大于X的最大整数值
mysql> select floor(-2.45);
+--------------+
| floor(-2.45) |
+--------------+
| -3 |
+--------------+
1 row in set (0.00 sec)
MOD(N,M):模操作,返回N被M除后的余数。
mysql> select mod(3,2);
+----------+
| mod(3,2) |
+----------+
| 1 |
+----------+
1 row in set (0.00 sec)
RAND()/RAND(N) :返回一个随机浮点值数a,范围在 0 到1 之间 (即, 其范围为 0 ≤ a ≤ 1.0)。若已指定一个整数参数 N,则它被用作种子值,用来产生重复序列。
mysql> select rand();
+-------------------+
| rand() |
+-------------------+
| 0.294932589209576 |
+-------------------+
1 row in set (0.00 sec)
mysql> select rand(2);
+-------------------+
| rand(2) |
+-------------------+
| 0.655586646549019 |
+-------------------+
1 row in set (0.00 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.00 sec)
mysql> select round(2.432,2 );
+-----------------+
| round(2.432,2 ) |
+-----------------+
| 2.43 |
+-----------------+
1 row in set (0.00 sec)
mysql> select round(12.432,-1 );
+-------------------+
| round(12.432,-1 ) |
+-------------------+
| 10 |
+-------------------+
1 row in set (0.00 sec)
以上就是小编为大家带来的mysql常用日期时间/数值函数详解(必看)全部内容了,希望大家多多支持~
mysql,日期函数
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新动态
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]