海詩網(wǎng) 知識百科 mysql(mysql數(shù)據(jù)庫教程)

mysql(mysql數(shù)據(jù)庫教程)

mysql, 如何在mysql中獲取當前時間?不知道小伙伴們今天來看看邊肖的分享吧!

mysql中獲取當前時間的函數(shù)是:now()。除了now()的功能外,還有其他功能:

current_timestamp()wbrwbrcurrent_timestamp/wbr/wbr

localtime()wbrwbrlocaltime/wbr/wbr

localtimestamp()wbrwbrlocaltimestampwbrwbrwbrwbr/wbr/wbr/wbr/wbr/wbr/wbr

這些日期時間函數(shù)都相當于now()。由于now()函數(shù)簡短易記,建議始終使用now()代替上面列出的函數(shù)。

mysql數(shù)據(jù)庫教程

擴展信息:

1.獲取當前日期時間函數(shù):sysdate()

sysdate()的日期時間函數(shù)與now()類似,但區(qū)別在于now()在執(zhí)行開始時獲取值,sysdate()在函數(shù)執(zhí)行時動態(tài)獲取值。

wbr/wbr

2.獲取當前日期函數(shù):curdate()

其中,以下兩個日期函數(shù)相當于curdate (): current _ date()和current _ date。

wbr/wbr

3.獲取當前時間函數(shù):curtime()

其中,以下兩個時間函數(shù)相當于curtime (): current _ time()和current _ time。

wbr/wbr

4.獲取當前的UTC日期時間函數(shù):UTC _ date(),UTC _ time(),UTC _ timestamp()。

因為中國位于東巴時區(qū),當?shù)貢r間=8小時UTC時間。當業(yè)務涉及多個國家和地區(qū)時,UTC時間非常有用。

wbr/wbr

mysql數(shù)據(jù)庫教程

MySQL日期時間提取函數(shù):

1.選擇日期和時間的不同部分:日期、時間、年、季度、月、日、小時、分鐘、秒和微秒。

set @dt=2008-09-10 07:15:30.123456;

wbr/wbr

select date(@dt); -- 2008-09-10

select time(@dt); -- 07:15:30.123456

select year(@dt); -- 2008

select quarter(@dt); -- 3

select month(@dt); -- 9

select week(@dt); -- 36

select day(@dt); -- 10

select hour(@dt); -- 7

select minute(@dt); -- 15

select second(@dt); -- 30

select microsecond(@dt); -- 123456

wbr/wbr

2.MySQL Extract()函數(shù),可以實現(xiàn)類似的功能:

set @dt=2008-09-10 07:15:30.123456;

wbr/wbr

select extract(year from @dt); -- 2008

select extract(quarter from @dt); -- 3

select extract(month from @dt); -- 9

select extract(week from @dt); -- 36

select extract(day from @dt); -- 10

select extract(hour from @dt); -- 7

select extract(minute from @dt); -- 15

select extract(second from @dt); -- 30

select extract(microsecond from @dt); -- 123456

select extract(year_month from @dt); -- 200809

select extract(day_hour from @dt); -- 1007

select extract(day_minute from @dt); -- 100715

select extract(day_second from @dt); -- 10071530

select extract(day_microsecond from @dt); -- 10071530123456

select extract(hour_minute from @dt); -- 715

select extract(hour_second from @dt); -- 71530

select extract(hour_microsecond from @dt); -- 71530123456

select extract(minute_second from @dt); -- 1530

select extract(minute_microsecond from @dt); -- 1530123456

select extract(second_microsecond from @dt); -- 30123456

MySQL Extract()函數(shù)具有除date()和time()之外的所有函數(shù)。并且還具有選擇‘日_微秒’的功能。

注意,這里不僅選擇了日和微秒,還選擇了從日期的日部分到微秒部分的所有時間。

MySQL Extract()函數(shù)唯一不好的地方就是需要敲幾次鍵盤。

wbr/wbr

mysql數(shù)據(jù)庫教程

3.MySQL dayof…函數(shù):dayofweek(),dayofmonth(),dayofyear()

分別返回日期參數(shù)在一周、一月和一年中的位置。

set @dt=2008-08-08;

select dayofweek(@dt); -- 6

select dayofmonth(@dt); -- 8

select dayofyear(@dt); -- 221

日期“2008-08-08”是一周的第6天(1=星期日,2=星期一,…,7=星期六);一月的第八天;一年的第221天。

wbr/wbr

4.關系型數(shù)據(jù)庫周…函數(shù):week()、weekofyear()、dayofweek()、weekday()、yearweek()

set @dt=2008-08-08;

select week(@dt); -- 31

select week(@dt,3); -- 32

select weekofyear(@dt); -- 32

select dayofweek(@dt); -- 6

select weekday(@dt); -- 4

select yearweek(@dt); -- 200831

MySQL week()函數(shù)可以有兩個參數(shù),詳見手冊。Weekofyear()和week()一樣,是計算“某一天”所在的一年中的第幾周。

What week of the year (@dt) is equivalent to week (@dt, 3).

Working days: (0=Monday, 1=Tuesday, …, 6=Sunday); Day of the week: (1=Sunday, 2=Monday, …, 7=Saturday)

MySQL yearweek () function, returns the year (2008) week position (31).

wbr/wbr

5.MySQL返回日和月名函數(shù):dayname()、month name()。

set @dt=2008-08-08;

select dayname(@dt); -- Friday

select monthname(@dt); -- August

wbr/wbr

6.MySQL last_day()函數(shù):返回一個月的最后一天。

select last_day(2008-02-01); -- 2008-02-29

select last_day(2008-08-08); -- 2008-08-31

wbr/wbr

wbr/wbr

mysql,以上就是本文為您收集整理的mysql最新內(nèi)容,希望能幫到您!更多相關內(nèi)容歡迎關注。

本文來自網(wǎng)絡,不代表海詩網(wǎng)立場,轉(zhuǎn)載請注明出處:http://x91880.com/n/169293.html
      

計算機網(wǎng)絡自頂向下方法(計算機網(wǎng)絡自頂向下方法習題答案)

發(fā)表回復
聯(lián)系我們
聯(lián)系我們

在線咨詢: QQ交談

郵箱: 3587015498@qq.com

工作時間:周一至周五,9:00-17:30,節(jié)假日休息

關注微信
微信掃一掃關注我們
微信掃一掃關注我們
關注微博
返回頂部