1. 程式人生 > >MySql常用日期時間函式

MySql常用日期時間函式

返回當前的日期和時間

select SYSDATE(); --2018-11-13 17:57:37 select NOW(); --2018-11-13 17:57:37 select CURDATE(); --2018-11-13 select CURTIME(); --17:57:37

 返回當前的日期

select MINUTE(SYSDATE()); --57 select HOUR(SYSDATE()); --17 select week(SYSDATE()); --45 select MONTH(SYSDATE()); --11 select MONTHNAME(SYSDATE()); --November select year(SYSDATE()); --2018

返回當前日期和時間的UNIX時間戳

select UNIX_TIMESTAMP(); --1542099027

select UNIX_TIMESTAMP(SYSDATE()); --1542099027

select FROM_UNIXTIME(unix_timestamp(sysdate())); --2018-11-13 16:50:27

select FROM_UNIXTIME(unix_timestamp(sysdate()), '%Y-%m-%d %H:%i:%S'); --2018-11-13 16:50:28

select FROM_UNIXTIME(unix_timestamp(sysdate())-20, '%Y-%m-%d %H:%i:%S'); --2018-11-13 16:50:08

參考格式如下:

%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), 這裡星期一是星期的第一天 %% 一個文字“%”。

用這個函式可以幫助我們在時間戳中篩選出某些天的資料。

SELECT username, FROM_UNIXTIME(create_time, "%Y-%m-%d") AS dat FROM `wp_user` WHERE create_time >= UNIX_TIMESTAMP('2017-11-29') AND create_time < UNIX_TIMESTAMP('2017-11-30') GROUP BY dat;

這個查詢可以讓我們查出29號那一天的使用者註冊記錄。

查詢前20秒內的記錄數量

select marketing_activity_id, ip_address, COUNT(ip_address) num  from rod_redpacket_log t1 where t1.create_date between FROM_UNIXTIME(unix_timestamp(sysdate())-20, '%Y-%m-%d %H:%i:%S') and sysdate() GROUP BY ip_address 

str_to_date (字串轉換為日期)函式:str_to_date(str, format) 

select str_to_date('08/09/2008', '%m/%d/%Y'); -- 2008-08-09 select str_to_date('08/09/08' , '%m/%d/%y'); -- 2008-08-09 select str_to_date('08.09.2008', '%m.%d.%Y'); -- 2008-08-09 select str_to_date('08:09:30', '%h:%i:%s'); -- 08:09:30 select str_to_date('08.09.2008 08:09:30', '%m.%d.%Y %h:%i:%s'); -- 2008-08-09 08:09:30

日期轉換函式、時間轉換函式 date_format(date,format), time_format(time,format)

select date_format('2008-08-08 22:23:01', '%Y%m%d%H%i%s'); select date_format(SYSDATE(), '%Y-%m-%d %H:%i:%s');

MySQL 日期、時間轉換函式:date_format(date,format), time_format(time,format) 能夠把一個日期/時間轉換成各種各樣的字串格式。它是 str_to_date(str,format) 函式的 一個逆轉換。   

MySQL 拼湊日期、時間函式:makdedate(year,dayofyear), maketime(hour,minute,second) 

select makedate(2001,31); -- '2001-01-31' select makedate(2001,32); -- '2001-02-01' select maketime(12,15,30); -- '12:15:30'

MySQL (時間、秒)轉換函式:time_to_sec(time), sec_to_time(seconds) 

select time_to_sec('01:00:05'); -- 3605 select sec_to_time(3605); -- '01:00:05'

日期時間計算函式

 select date_add(now(), interval 1 day); -- add 1 day select date_add(now(), interval 1 hour); -- add 1 hour select date_add(now(), interval 1 minute); -- ... select date_add(now(), interval 1 second); select date_add(now(), interval 1 microsecond); select date_add(now(), interval 1 week); select date_add(now(), interval 1 month); select date_add(now(), interval 1 quarter); select date_add(now(), interval 1 year);

select date_add(now(), interval -1 day); -- sub 1 day

set @dt = '2008-08-09 12:12:33'; select date_add(@dt, interval '01:15:30' hour_second); --2008-08-09 13:28:03 select date_add(@dt, interval '1 01:15:30' day_second); --2008-08-10 13:28:03

 日期減去一個時間間隔:date_sub()

select date_sub('1998-01-01 00:00:00', interval '1 1:1:1' day_second); --1997-12-30 22:58:59

日期、時間相減函式:datediff(date1,date2), timediff(time1,time2) ,date1-date2返回天數,兩個引數型別必須相同。

select datediff('2008-08-08', '2008-08-01'); -- 7 select datediff('2008-08-01', '2008-08-08'); -- -7

 時間戳(timestamp)轉換、增、減函式

select timestamp('2008-08-08'); -- 2008-08-08 00:00:00 select timestamp('2008-08-08 08:00:00', '01:01:01'); -- 2008-08-08 09:01:01 select timestamp('2008-08-08 08:00:00', '10 01:01:01'); -- 2008-08-18 09:01:01

select timestampadd(day, 1, '2008-08-08 08:00:00'); -- 2008-08-09 08:00:00 select date_add('2008-08-08 08:00:00', interval 1 day); -- 2008-08-09 08:00:00

# MySQL timestampadd() 函式類似於 date_add()。 select timestampdiff(year, '2002-05-01', '2001-01-01'); -- -1 select timestampdiff(day , '2002-05-01', '2001-01-01'); -- -485 select timestampdiff(hour, '2008-08-08 12:00:00', '2008-08-08 00:00:00'); -- -12

select datediff('2008-08-08 12:00:00', '2008-08-01 00:00:00'); -- 7

 MySQL 獲得當前時間戳函式:current_timestamp, current_timestamp() 

select current_timestamp, current_timestamp(); --2018-11-13 17:23:56,2018-11-13 17:23:56