天天看点

mysql常用日期和时间函数

好久没写博客了,QAQ

1、获取当前时间 now() sysdate()

select now()

2018-08-09 18:30:15

select sysdate()

2018-08-09 18:31:39

以上两个的区别是now()是在sql语句执行时就获取到了时间,也就是你点击运行时的时间

而sysdate()是获取执行到sysdate()的时间

select now(),sleep(3),now();

2018-08-09 18:34:18 \ 2018-08-09 18:34:18

select sysdate(),sleep(3),sysdate() ;

2018-08-09 18:36:04 \ 2018-08-09 18:36:01

sleep(int second)指延时多少秒

2、获取当前时间戳函数

select current_timestamp

2018-08-09 18:40:01

select current_timestamp()

2018-08-09 18:40:01

3、获取当前时分秒

select current_time

18:38:49

4、Unix 时间戳、日期转换函数

unix_timestamp(),

unix_timestamp(date),

from_unixtime(unix_timestamp),

from_unixtime(unix_timestamp,format)

select unix_timestamp()

1533811373

select unix_timestamp(“2018-8-8”)

1533657600

select from_unixtime(1533657600,”%Y %D %M %h:%i:%s %x”)

2018 8th August 12:00:00 2018

5、date_add() date_sub() datediff()函数

select datediff(“2015-11-16”,now())

-997

select timestampdiff(day,”2015-11-16”,now())

997

具体可参考http://www.cnblogs.com/ggjucheng/p/3352280.html,感谢!!!!

继续阅读