Mysql函式記錄

2021-10-16 15:40:48 字數 2575 閱讀 4360

1.當前時間,精確到時分秒

now

()

2.當前日期,精確到日(年月日)

curdate

()

3.當前年份

year

(now()

)

4.當前月

month

(日期字段)

5.年月日

from_unixtime

(日期字段,

'%y'

) 年 from_unixtime

(日期字段,

'%m'

) 月 from_unixtime

(日期字段,

'%y-%m-%d'

) 年月日

6.年月日

date_format

(日期字段,

'%y%m%d'

) 年月日

7.最近時間

max

(日期字段)

8.十分鐘之內的資料

日期字段》=

date_sub

(now()

,interval 10 minute)

9.查詢日期所在季節

concat

(floor((

date_format

(日期字段,

'%m')+

2)/3

)) sale

weekday() 函式和 dayofweek() ,date_format(curdate(),』%w』)類似,

都是返回「某天」在一周中的位置。

不同點在於參考的標準,

weekday:(0 = monday, 1 = tuesday, …, 6 = sunday);

dayofweek:(1 = sunday, 2 = monday, …, 7 = saturday)

%w 是以數字的形式來表示週中的天數( 0 = sunday, 1=monday, . . ., 6=saturday),

0為週日,6為週六,跟我們一般的認知,一周是從周一開始的並不一樣。

10.獲取當前日期在本週的周一

方法一:

select subdate

(now()

,weekday

(now()

));

方法二:

(如果今天是週日獲取的將是下週周一,一般適用於國外)

select subdate

(curdate()

,date_format

(curdate()

,'%w')-

1)所以要進行判斷處理

select subdate

(curdate()

,if(date_format

(curdate()

,'%w')=

0,7,

date_format

(curdate()

,'%w'))

-1)

11.獲取當前日期在本週的週日

方法一:

select subdate

(now()

,weekday

(now()

)-6)

;

方法二:

(如果今天是週日獲取的將是下週週日,一般適用於國外)

select subdate

(curdate()

,date_format

(curdate()

,'%w')-

7)同樣要進行判斷處理

select subdate

(curdate()

,if(date_format

(curdate()

,'%w')=

0,7,

date_format

(curdate()

,'%w'))

-7)

12.當前日期前一天,也就是昨天

select subdate

(now()

,1) as 'yesterday'

13.當前日期後一天,也就是明天

select subdate

(now()

, interval -

1 day) as 'tomorrow'

mysql 記錄函式 mysql 函式記錄

條件查詢 case case when then when then when then else end1.簡單case函式寫法 注意 的位置 select case when 1 then 男 when 0 then 女 else 保密 end as text from user 2.case搜...

Mysql函式記錄

1.instr srt,str 函式,得到字元第一次出現所在的位置,位置從1開始 select instr 1234 3 3 2.substring str,num 擷取字串,從索引位置擷取,包括索引位置,位置從1開始 select substring 123 2 23 3.concat str,連...

Mysql 函式記錄

1.一行轉多行 substring index 1 將一行拆分為多行 select a.id,substring index substring index a.ids,b.help topic id 1 1 as ids from inini a join mysql.help topic b o...