主頁 > 知識庫 > mysql中TIMESTAMPDIFF案例詳解

mysql中TIMESTAMPDIFF案例詳解

熱門標簽:云南電商智能外呼系統(tǒng)價格 大眾點評星級酒店地圖標注 臨清電話機器人 高清地圖標注道路 拉卡拉外呼系統(tǒng) 智能外呼系統(tǒng)復位 外東北地圖標注 400電話可以辦理嗎 話務外呼系統(tǒng)怎么樣

1.  Syntax

TIMESTAMPDIFF(unit,begin,end); 根據(jù)單位返回時間差,對于傳入的begin和end不需要相同的數(shù)據(jù)結(jié)構(gòu),可以存在一個為Date一個DateTime

2. Unit

支持的單位有

  • MICROSECOND
  • SECOND
  • MINUTE
  • HOUR
  • DAY
  • WEEK
  • MONTH
  • QUARTER
  • YEAR

3. Example

下面這個例子是對于TIMESTAMPDIFF最基本的用法,

  • 3.1 求 2017-01-01 - 2017-02-01 之間有幾個月
SELECT TIMESTAMPDIFF(MONTH, '2017-01-01', '2017-02-01') as  result;

+--------+
| result |
+--------+
| 1 |
+--------+
1 row in set (0.00 sec)
  • 3.2 求 2017-01-01 - 2017-02-01 之間有幾天
SELECT TIMESTAMPDIFF(DAY, '2017-01-01', '2017-02-01') as  result;

+--------+
| result |
+--------+
| 31 |
+--------+
1 row in set (0.00 sec)
  •  3.3 求 2017-01-01 08: 00:00 - 2017-01-01 08: 55:00 之間有幾分鐘
SELECT TIMESTAMPDIFF(MINUTE, '2017-01-01 08:00:00', '2017-01-01 08:55:00') result;

+--------+
| result |
+--------+
| 55 |
+--------+
1 row in set (0.00 sec)
  •  3.4 求 2017-01-01 08: 00:00 - 2017-01-01 08: 55:33 之間有幾分鐘
SELECT TIMESTAMPDIFF(MINUTE, '2017-01-01 08:00:00', '2017-01-01 08:55:33') result;

+--------+
| result |
+--------+
| 55 |
+--------+
1 row in set (0.00 sec)
  •  3.5 對于DAY, MINUTE進行計算DIFF時,會直接將相對應的DAY,MINUTE相減

  • 3.6 對于 SECOND 會怎樣計算呢
SELECT TIMESTAMPDIFF(SECOND, '2017-01-01 08:00:00', '2017-01-01 08:55:33') result;

55 * 60 + 33 = 3333
+--------+
| result |
+--------+
|   3333 |
+--------+
1 row in set (0.00 sec)
  • 3.7 如何求數(shù)據(jù)庫中兩個date字段的diff
    • 3.7.1  建表
      CREATE TABLE demo (id INT AUTO_INCREMENT PRIMARY KEY, start_time DATE NOT NULL, end_time DATE NOT NULL);
      Query OK, 0 rows affected (0.10 sec)
    • 3.7.2  添加數(shù)據(jù)
      INSERT INTO demo(start_time, end_time)
      VALUES('1983-01-01', '1990-01-01'),
      ('1983-01-01', '1989-06-06'),
      ('1983-01-01', '1985-03-02'),
      ('1983-01-01', '1992-05-05'),
      ('1983-01-01 11:12:11', '1995-12-01');
    • 3.7.3 直接query數(shù)據(jù)
      select * from demo;
      +----+------------+------------+
      | id | start_time | end_time |
      +----+------------+------------+
      | 1 | 1983-01-01 | 1990-01-01 |
      | 2 | 1983-01-01 | 1989-06-06 |
      | 3 | 1983-01-01 | 1985-03-02 |
      | 4 | 1983-01-01 | 1992-05-05 |
      | 5 | 1983-01-01 | 1995-12-01 |
      +----+------------+------------+
      5 rows in set (0.00 sec)
    • 3.7.4 計算duration
      select *, TIMESTAMPDIFF(YEAR, start_time, end_time) as duration from demo;
      
      +----+------------+------------+----------+
      | id | start_time | end_time | duration |
      +----+------------+------------+----------+
      | 1 | 1983-01-01 | 1990-01-01 | 7 |
      | 2 | 1983-01-01 | 1989-06-06 | 6 |
      | 3 | 1983-01-01 | 1985-03-02 | 2 |
      | 4 | 1983-01-01 | 1992-05-05 | 9 |
      | 5 | 1983-01-01 | 1995-12-01 | 12 |
      +----+------------+------------+----------+
      5 rows in set (0.00 sec)
    • 3.7.5 其他應用
      select *, if(TIMESTAMPDIFF(YEAR, end_time, CURRENT_TIMESTAMP()) 26 ,' 26','>= 26') as result from demo;
      
      +----+------------+------------+--------+
      | id | start_time | end_time | result |
      +----+------------+------------+--------+
      | 1 | 1983-01-01 | 1990-01-01 | >= 26 |
      | 2 | 1983-01-01 | 1989-06-06 | >= 26 |
      | 3 | 1983-01-01 | 1985-03-02 | >= 26 |
      | 4 | 1983-01-01 | 1992-05-05 |  26 |
      | 5 | 1983-01-01 | 1995-12-01 |  26 |
      +----+------------+------------+--------+
      5 rows in set (0.00 sec)

到此這篇關(guān)于mysql中TIMESTAMPDIFF案例詳解的文章就介紹到這了,更多相關(guān)mysql中TIMESTAMPDIFF內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • MySQL的時間差函數(shù)TIMESTAMPDIFF、DATEDIFF的用法
  • MySQL的時間差函數(shù)(TIMESTAMPDIFF、DATEDIFF)、日期轉(zhuǎn)換計算函數(shù)(date_add、day、date_format、str_to_date)
  • 實現(xiàn)MySQL + PostgreSQL批量插入更新insertOrUpdate
  • MYSQL row_number()與over()函數(shù)用法詳解
  • MySQL隱式類型轉(zhuǎn)換導致索引失效的解決
  • 淺談MySQL數(shù)據(jù)查詢太多會OOM嗎
  • Django搭建MySQL主從實現(xiàn)讀寫分離
  • MySQL多表連接查詢詳解

標簽:山西 無錫 三明 溫州 福州 定西 阿里 揚州

巨人網(wǎng)絡通訊聲明:本文標題《mysql中TIMESTAMPDIFF案例詳解》,本文關(guān)鍵詞  mysql,中,TIMESTAMPDIFF,案例,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《mysql中TIMESTAMPDIFF案例詳解》相關(guān)的同類信息!
  • 本頁收集關(guān)于mysql中TIMESTAMPDIFF案例詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章