MySQL 日期時間相關函式整理

2021-09-07 08:29:43 字數 4275 閱讀 7805

-- 為日期增加乙個時間間隔:date_add()

select

now();

select date_add(now(),interval -

1year

);select date_add(now(),interval -

1month

);select date_add(now(),interval 28

day);

select date_add(now(),interval -

6hour);

select date_add(now(),interval -

999minute);

select date_add(now(),interval 23

second);

select date_add(now(),interval 2

microsecond);

select date_add(now(),interval 1

week);

select date_add(now(),interval 0 quarter);

-- 為日期減去乙個時間間隔:date_sub()

select date_sub(now(),interval 1

year

);select date_sub(now(),interval 1

month

);select date_sub(now(),interval 1

day);

select date_sub(now(),interval 1

hour);

select date_sub(now(),interval 1

minute);

select date_sub(now(),interval 1

second);

select date_sub(now(),interval 1

microsecond);

select date_sub(now(),interval 1

quarter);

-- 給定乙個日期date,返回乙個從年份為0開始的天數 :to_days()

select to_days("0000-01

-01");select to_days(now());

-- 擷取資料庫某一列欄位中的一部分 substr()

select substr(now(),1,13);

-- 今天

select

*from 表名 where to_days(時間欄位名) = to_days(now());

-- 昨天

select

*from 表名 where to_days( now( ) ) - to_days( 時間欄位名) <=

1

-- 近7天

select

*from 表名 where date_sub(curdate(), interval 7

day) <= date(時間欄位名)

-- 近30天

select

*from 表名 where date_sub(curdate(), interval 30

day) <= date(時間欄位名)

-- 本月

select

*from 表名 where date_format( 時間欄位名, '

%y%m

' ) = date_format( curdate( ) , '

%y%m

' )

-- 上個月

select

*from 表名 where period_diff( date_format( now( ) , '

%y%m

' ) , date_format( 時間欄位名, '

%y%m

' ) ) =

1

-- 查詢本季度資料

select

*from `ht_invoice_information` where quarter(create_date)=quarter(now());

-- 查詢上季度資料

select

*from `ht_invoice_information` where quarter(create_date)=quarter(date_sub(now(),interval 1 quarter));

-- 查詢本年資料

select

*from `ht_invoice_information` where

year(create_date)=

year(now());

-- 查詢上年資料

select

*from `ht_invoice_information` where

year(create_date)=

year(date_sub(now(),interval 1

year));

-- 查詢本週資料

select name,submittime from enterprise where yearweek(date_format(submittime,'

%y-%m-%d

')) = yearweek(now());

-- 查詢上週資料

select name,submittime from enterprise where yearweek(date_format(submittime,'

%y-%m-%d

')) = yearweek(now())-

1;

-- 查詢上月資料

select name,submittime from enterprise where date_format(submittime,'

%y-%m

')=date_format(date_sub(curdate(), interval 1

month),'

%y-%m')

select

*from

user

where date_format(pudate,'

%y%m

') = date_format(curdate(),'

%y%m

') ;

select

*from

user

where weekofyear(from_unixtime(pudate,'

%y-%m-%d

')) =

weekofyear(now())

select

*from

user

where

month(from_unixtime(pudate,'

%y-%m-%d

')) =

month

(now())

select

*from

user

where

year(from_unixtime(pudate,'

%y-%m-%d

')) =

year(now()) and

month(from_unixtime(pudate,'

%y-%m-%d

')) =

month

(now())

select

*from

user

where pudate between 上月最後一天 and 下月第一天

-- 查詢本月資料

select name,submittime from enterprise   where date_format(submittime,'

%y-%m

')=date_format(now(),'

%y-%m

')

-- 查詢距離當前時間6個月的資料

select name,submittime from enterprise where submittime between date_sub(now(),interval 6

month) and now();

啦啦啦

啦啦啦

函式整理(日期和時間函式)

函式整理 日期和時間函式 日期和時間函式處理支援日期 時間值轉換的各種過程。一星期七天用下面的 值表示。值 一星期七天 1 星期日 2 星期一 3 星期二 4 星期三 5 星期四 6 星期五 7 星期六 cdate cdate函式轉換成date子類的表示式。用法為 result cdate expr...

MySQL日期相關函式

1.獲取當前日期 curdate current date current date mysql select curdate mysql select curtime 另外獲取utc 全球吧標準時間,原先也被稱作格林威治標準時間或gmt 時間,本地時間 utc 時間 8 小時,相應的函式是 utc...

MySQL 時間日期型別和相關函式

mysql中的日期和時間型別 型別位元組 最小值最大值 零值datetime 81000 01 01 00 00 00 9999 12 31 23 59 59 0000 00 00 00 00 00 date 41000 01 01 9999 12 31 0000 00 00 timestamp 4...