mysql時間相減的問題

來源:互聯網
上載者:User

標籤:style   sp   strong   on   div   問題   bs   amp   時間   

MySQL中時間不能直接相減,如果日、分、時不同,相減結果是錯誤的 

mysql> select t1,t2,t2-t1 from mytest;  +---------------------+---------------------+-------+  | t1                  | t2                  | t2-t1 |  +---------------------+---------------------+-------+  | 2013-04-21 16:59:33 | 2013-04-21 16:59:43 |    10 |  | 2013-04-21 16:59:33 | 2013-04-21 17:00:33 |  4100 |  | 2013-04-21 16:59:33 | 2013-04-21 17:59:35 | 10002 |  +---------------------+---------------------+-------+    實際是mysql的時間相減是做了一個隱式轉換操作,將時間轉換為整數,但並不是用unix_timestamp轉換,而是直接把年月日時分秒拼起來,如2013-04-21 16:59:33 直接轉換為20130421165933,由於時間不是十進位,所以最後得到的結果沒有意義,這也是導致上面出現坑爹的結果。  要得到正確的時間相減秒值,有以下3種方法: 1、time_to_sec(timediff(t2, t1)), 2、timestampdiff(second, t1, t2), 3、unix_timestamp(t2) -unix_timestamp(t1)

mysql時間相減的問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.