久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

如何在mysql中使用日期處理函數

131次閱讀
沒有評論

共計 1251 個字符,預計需要花費 4 分鐘才能閱讀完成。

自動寫代碼機器人,免費開通

本篇文章給大家分享的是有關如何在 mysql 中使用日期處理函數,丸趣 TV 小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著丸趣 TV 小編一起來看看吧。

首先創建一張實驗用的一張表

drop table if exists t_student;
create table t_student(
 id int primary key auto_increment,
 name varchar(20) not null comment  姓名 ,
 birthday date comment  生日 
)Engine=InnoDB default charset utf8;

insert into t_student values(null, tom , 1992-02-03 insert into t_student values(null, jerry , 1993-02-06 insert into t_student values(null, hank , 1993-03-05 insert into t_student values(null, xiaoming ,now());

其中 date 類型 是記錄 mysql 精確日期的類型

now() 函數

獲取當前時間

如何在 mysql 中使用日期處理函數

year() , month(),dayofmonth()

上面三個函數是分別從一個日期或者時間中提取出年,月,日

比如 想得到生日為 2 月份的學生

select * from t_student where month(birthday) = 2;

如何在 mysql 中使用日期處理函數

monthname() 函數

輸出個月份的英文單詞

select monthname(birthday) from t_student;

如何在 mysql 中使用日期處理函數

timestampdiff() 函數

比較兩個日期間的差值

例:學生的年齡

select timestampdiff(year,birthday ,now()) as age from t_student;

如何在 mysql 中使用日期處理函數

timestampdiff 函數的第一個參數為 計算結果的單位:有 year(年) month(月),day(日) 等等。

to_days()

將日期轉換成天數

計算兩個時間的天數, 同 timestampdiff(day,arg1,arg2) 是一個道理。

查詢生日小于當前日期 60 以內的學生

select * from t_student where (to_days(now()) – to_days(birthday))

如何在 mysql 中使用日期處理函數

date_add 和 date_sub

根據一個日期,計算出另一個日期,date_add 是加上 date_sub 是減去

select date_add(1970-1-1 , interval 10 year); # 1970 年 加上 10 年

如何在 mysql 中使用日期處理函數

select date_sub(1970-1-1 , interval 10 year); #1970 年減去 10 年

如何在 mysql 中使用日期處理函數

以上就是如何在 mysql 中使用日期處理函數,丸趣 TV 小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注丸趣 TV 行業資訊頻道。

向 AI 問一下細節

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-12-04發表,共計1251字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 福州市| 阿勒泰市| 平江县| 澜沧| 廉江市| 琼海市| 汕头市| 中阳县| 香河县| 门头沟区| 皮山县| 石泉县| 德化县| 金坛市| 吉木乃县| 夹江县| 定州市| 道孚县| 萝北县| 天等县| 通道| 丽水市| 高要市| 革吉县| 乌拉特中旗| 萨迦县| 固阳县| 赞皇县| 蒙阴县| 兴文县| 库车县| 张掖市| 兰坪| 荔波县| 广灵县| 库尔勒市| 阳城县| 清涧县| 禹州市| 伊川县| 财经|