自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

MySQL數(shù)據(jù)庫(kù)的日期函數(shù)與時(shí)間函數(shù)

數(shù)據(jù)庫(kù) MySQL
我們今天主要和大家一起分享的是MySQL數(shù)據(jù)庫(kù)的日期函數(shù)與時(shí)間函數(shù)(MySQL 5.X)的實(shí)際應(yīng)用的經(jīng)驗(yàn)總結(jié),下面就是文章的詳細(xì)內(nèi)容描述。

以下的文章主要講述的是MySQL數(shù)據(jù)庫(kù)的日期函數(shù)與時(shí)間函數(shù)(MySQL 5.X)的實(shí)際應(yīng)用的經(jīng)驗(yàn)總結(jié),MySQL日期函數(shù)與時(shí)間函數(shù)在實(shí)際應(yīng)用中出現(xiàn)的頻率還是很大的,以下的文章就是對(duì)這兩個(gè)函數(shù)的詳細(xì)描述。

MySQL 數(shù)據(jù)庫(kù)獲得當(dāng)前日期時(shí)間 函數(shù):

1.1 獲得當(dāng)前日期+時(shí)間(date + time)函數(shù):now()

 

  1. mysql> select now();  
  2. +---------------------+  
  3. | now() |  
  4. +---------------------+  
  5. | 2008-08-08 22:20:46 |  
  6. +---------------------+  

 

除了 now() 函數(shù)能獲得當(dāng)前的日期時(shí)間外,MySQL 中還有下面的函數(shù):

 

  1. current_timestamp()  
  2. ,current_timestamp  
  3. ,localtime()  
  4. ,localtime  
  5. ,localtimestamp -- (v4.0.6)  
  6. ,localtimestamp() -- (v4.0.6) 

這些日期時(shí)間函數(shù),都等同于 now()。鑒于 now() 函數(shù)簡(jiǎn)短易記,建議總是使用 now() 來(lái)替代上面列出的函數(shù)。

1.2 獲得當(dāng)前日期+時(shí)間(date + time)函數(shù):sysdate()

sysdate() 日期時(shí)間函數(shù)跟 now() 類似,不同之處在于:now() 在執(zhí)行開始時(shí)值就得到了, sysdate() 在函數(shù)執(zhí)行時(shí)動(dòng)態(tài)得到值。看下面的例子就明白了:

 

  1. mysql> select now(), sleep(3), now();  
  2. +---------------------+----------+---------------------+  
  3. | now() | sleep(3) | now() |  
  4. +---------------------+----------+---------------------+  
  5. | 2008-08-08 22:28:21 | 0 | 2008-08-08 22:28:21 |  
  6. +---------------------+----------+---------------------+mysql> select sysdate(), sleep(3), sysdate();  
  7. +---------------------+----------+---------------------+  
  8. | sysdate() | sleep(3) | sysdate() |  
  9. +---------------------+----------+---------------------+  
  10. | 2008-08-08 22:28:41 | 0 | 2008-08-08 22:28:44 |  
  11. +---------------------+----------+---------------------+  

可以看到,雖然 中途 sleep 3 秒,但 now() 函數(shù)兩次的時(shí)間值是相同的; sysdate() 函數(shù)兩次得到的時(shí)間值相差 3 秒。MySQL 數(shù)據(jù)庫(kù)Manual 中是這樣描述 sysdate() 的:Return the time at which the function executes。

sysdate() 日期時(shí)間函數(shù),一般情況下很少用到。

2. 獲得當(dāng)前日期(date)函數(shù):curdate()

 

  1. mysql> select curdate();  
  2. +------------+  
  3. | curdate() |  
  4. +------------+  
  5. | 2008-08-08 |  
  6. +------------+  

 

其中,下面的兩個(gè)日期函數(shù)等同于 curdate():

current_date()
,current_date3. 獲得當(dāng)前時(shí)間(time)函數(shù):curtime()

 

  1. mysql> select curtime();  
  2. +-----------+  
  3. | curtime() |  
  4. +-----------+  
  5. | 22:41:30 |  

+-----------+其中,下面的兩個(gè)時(shí)間函數(shù)等同于 curtime():

current_time()

,current_time4. 獲得當(dāng)前 UTC 日期時(shí)間函數(shù):

  1. utc_date(), utc_time(), utc_timestamp()  
  2. mysql> select utc_timestamp(), utc_date(), utc_time(), now()  
  3. +---------------------+------------+------------+---------------------+  
  4. | utc_timestamp() | utc_date() | utc_time() | now() |  
  5. +---------------------+------------+------------+---------------------+  
  6. | 2008-08-08 14:47:11 | 2008-08-08 | 14:47:11 | 2008-08-08 22:47:11 |  
  7. +---------------------+------------+------------+---------------------+  

 

因 為我國(guó)位于東八時(shí)區(qū),所以本地時(shí)間 = UTC 時(shí)間 + 8 小時(shí)。UTC 時(shí)間在業(yè)務(wù)涉及多個(gè)國(guó)家和地區(qū)的時(shí)候,非常有用。

二、MySQL數(shù)據(jù)庫(kù) 日期時(shí)間 Extract(選?。?函數(shù)。

1. 選取日期時(shí)間的各個(gè)部分:日期、時(shí)間、年、季度、月、日、小時(shí)、分鐘、秒、微秒

 

  1. set @dt = '2008-09-10 07:15:30.123456';  
  2. select date(@dt); -- 2008-09-10  
  3. select time(@dt); -- 07:15:30.123456  
  4. select year(@dt); -- 2008  
  5. select quarter(@dt); -- 3  
  6. select month(@dt); -- 9  
  7. select week(@dt); -- 36  
  8. select day(@dt); -- 10  
  9. select hour(@dt); -- 7  
  10. select minute(@dt); -- 15  
  11. select second(@dt); -- 30  
  12. select microsecond(@dt);  

 

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

 

  1. set @dt = '2008-09-10 07:15:30.123456';  
  2.  
  3. select extract(year from @dt); -- 2008  
  4. select extract(quarter from @dt); -- 3  
  5. select extract(month from @dt); -- 9  
  6. select extract(week from @dt); -- 36  
  7. select extract(day from @dt); -- 10  
  8. select extract(hour from @dt); -- 7  
  9. select extract(minute from @dt); -- 15  
  10. select extract(second from @dt); -- 30  
  11. select extract(microsecond from @dt); -- 123456select extract(year_month from @dt); -- 200809  
  12. select extract(day_hour from @dt); -- 1007  
  13. select extract(day_minute from @dt); -- 100715  
  14. select extract(day_second from @dt); -- 10071530  
  15. select extract(day_microsecond from @dt); -- 10071530123456  
  16. select extract(hour_minute from @dt); -- 715  
  17. select extract(hour_second from @dt); -- 71530  
  18. select extract(hour_microsecond from @dt); -- 71530123456  
  19. select extract(minute_second from @dt); -- 1530  
  20. select extract(minute_microsecond from @dt); -- 1530123456  
  21. select extract(second_microsecond from @dt);  

30123456MySQL Extract() 函數(shù)除了沒有date(),time() 的功能外,其他功能一應(yīng)具全。并且還具有選取‘day_microsecond’ 等功能。注意這里不是只選取 day 和 microsecond,而是從日期的 day 部分一直選取到 microsecond 部分。夠強(qiáng)悍的吧!

以上的相關(guān)內(nèi)容就是對(duì)MySQL數(shù)據(jù)庫(kù)的日期函數(shù)、時(shí)間函數(shù)(MySQL 5.X)的部分內(nèi)容介紹,望你能有所收獲。

【編輯推薦】

  1. MySQL 4.1 數(shù)據(jù)轉(zhuǎn)換的指導(dǎo)
  2. 配置MySQL與卸載MySQL實(shí)操
  3. 圖解MySQL數(shù)據(jù)庫(kù)安裝與實(shí)際操作
  4. 卸載MySQL數(shù)據(jù)庫(kù)的實(shí)現(xiàn)需要哪些項(xiàng)目?
  5. MySQL數(shù)據(jù)庫(kù)進(jìn)行備份在Linux異構(gòu)網(wǎng)絡(luò)里
責(zé)任編輯:佚名 來(lái)源: 互聯(lián)網(wǎng)
相關(guān)推薦

2010-06-13 10:18:08

MySQL 數(shù)據(jù)庫(kù)函數(shù)

2010-11-25 11:01:33

MySQL日期函數(shù)

2010-05-27 13:26:20

MySQL日期時(shí)間函數(shù)

2009-02-24 16:17:41

日期時(shí)間函數(shù)

2010-10-09 15:46:56

MySQL日期時(shí)間

2024-03-05 15:26:03

日期函數(shù)數(shù)據(jù)庫(kù)MySQL

2010-10-09 14:42:38

MySQL獲取時(shí)間

2021-06-05 21:29:53

數(shù)據(jù)庫(kù)MySQL函數(shù)

2010-09-27 16:38:19

Sql Server日

2023-05-29 15:23:37

MySQL數(shù)據(jù)庫(kù)函數(shù)

2010-05-14 17:34:36

MySQL數(shù)據(jù)庫(kù)列值

2010-09-14 14:22:30

Sql Server日

2010-06-04 15:32:18

MySQL數(shù)據(jù)庫(kù)

2011-03-03 10:45:34

PureftpdMYSQL

2011-03-11 16:13:37

IBM DB2數(shù)據(jù)庫(kù)日期操作函數(shù)

2010-10-09 14:32:23

mysql函數(shù)

2010-10-09 15:07:35

MySQL日期

2023-03-30 08:00:56

MySQL日期函數(shù)

2010-07-12 11:38:24

SQL Server函

2010-06-18 10:50:55

ms SQL Serv
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)