A. timestampdiff() 傳三個參數,第一個時間類型如年,月,日,第二個開始時間,第三個結束時間
select test_name, timestampdiff(YEAR,create_time,end_time) y_date from test_table; --計算時間ide
+-----------+--------+
| test_name | y_date |
+-----------+--------+
| 哈羅德 | 29 |
| 哈羅德 | 17 |
| 格溫 | 13 |
| 班尼 | 110 |
+-----------+--------+orm
B. INTERVAL n day day還能夠替換爲年月等
select now()+ INTERVAL 1 day; --表示明天字符串
+-----------------------+
| now()+ INTERVAL 1 day |
+-----------------------+
| 2018-12-07 09:52:03 |
+-----------------------+it
C. last_day() 括號內加個時間日期,表示月末
select last_day(now()); --表示月末table
+-----------------+
| last_day(now()) |
+-----------------+
| 2018-12-31 |
+-----------------+ast
D. extract(day from now()) 表示某月第幾天,day 還能夠替換爲year,month 等
select date_sub(date(now()),interval extract(day from now()) - 1 day) as 'month_firstday'; 月初form
+----------------+
| month_firstday |
+----------------+
| 2018-12-01 |
+----------------+class
E. date_format() 把一個日期/時間轉換成各類各樣的字符串格式test
select date_format(now(),'%Y-%m-%d %h:%i:%s'); %Y:年,% m:月, %M:英文月,%d: 日, %D :英文日, %h:小時,%H:24小時制,%i:分鐘,%s:秒date
+----------------------------------------+
| date_format(now(),'%Y-%m-%d %h:%i:%s') |
+----------------------------------------+
| 2018-12-06 10:28:04 |
+----------------------------------------+
F. str_to_date() 把一個字符串格式日期/時間轉換成時間
select str_to_date('07.10.2017 08:09:30', '%m.%d.%Y %h:%i:%s') as st_to_d;
+---------------------+
| st_to_d |
+---------------------+
| 2017-07-10 08:09:30 |
+---------------------+
1 row in set (0.06 sec)
G. date_add(),date_sub()
select date_add(now(), interval 1 day) as add_day; 加一天, 表示明天
+---------------------+
| add_day |
+---------------------+
| 2018-12-07 10:46:50 |
+---------------------+
1 row in set (0.00 sec)
select date_add(now(), interval 1 day) as add_day; 減一天,表示昨天
+---------------------+
| sub_day |
+---------------------+
| 2018-12-05 10:48:48 |
+---------------------+
1 row in set (0.00 sec)
H. select week(now()); 年中第幾周
select quarter(now()); 季度
select dayofweek(now()); 一週中第幾天
select dayofmonth(now()); 一個月第幾天
select dayofyear(now()); 一年第幾天
show variables like 'lc_time_names';
select dayname(now()),monthname(now()); 英文周幾,英文月份
+----------------+------------------+
| dayname(now()) | monthname(now()) |
+----------------+------------------+
| Thursday | December |
+----------------+------------------+
set lc_time_names='zh_CN'; 時間名稱改成中文
+----------------+------------------+| dayname(now()) | monthname(now()) |+----------------+------------------+| 星期四 | 十二月 |+----------------+------------------+