zoukankan      html  css  js  c++  java
  • MySQL Date函数的正确用法

    以下的文章主要介绍的是MySQL Date函数的实际应用其中包括如何获取当前时间的具体操作,Unix时间的具体应用,时间前后、时间间隔与时间转换的实际内容描述,以下就是文章的主要内容。

    MySQL Date函数 1、获取当前时间

    1. MySQL> select current_timestamp();  
    2. +---------------------+  
    3. | current_timestamp() |  
    4. +---------------------+  
    5. | 2010-01-18 21:24:37 |  
    6. +---------------------+  
    7. 1 row in set (0.00 sec)  
    8. MySQL> select current_date();  
    9. +----------------+  
    10. | current_date() |  
    11. +----------------+  
    12. | 2010-01-18 |  
    13. +----------------+  
    14. 1 row in set (0.00 sec)  
    15. MySQL> select current_time();  
    16. +----------------+  
    17. | current_time() |  
    18. +----------------+  
    19. | 21:24:46 |  
    20. +----------------+  
    21. 1 row in set (0.00 sec)  

    MySQL Date函数 2、Unix时间

    1. MySQL> select unix_timestamp();  
    2. +------------------+  
    3. | unix_timestamp() |  
    4. +------------------+  
    5. | 1263821184 |  
    6. +------------------+  
    7. 1 row in set (0.00 sec)  
    8. MySQL> select from_unixtime(1263821182);  
    9. +---------------------------+  
    10. | from_unixtime(1263821182) |  
    11. +---------------------------+  
    12. | 2010-01-18 21:26:22 |  
    13. +---------------------------+  
    14. 1 row in set (0.00 sec)  

    MySQL Date函数 3、时间前后

    1. MySQL> select date_add(current_timestamp, interval 1 day);  
    2. +---------------------------------------------+  
    3. | date_add(current_timestamp, interval 1 day) |  
    4. +---------------------------------------------+  
    5. | 2010-01-19 21:27:53 |  
    6. +---------------------------------------------+  
    7. 1 row in set (0.00 sec)  
    8. MySQL> select date_add(current_time, interval 1 day);  
    9. +----------------------------------------+  
    10. | date_add(current_time, interval 1 day) |  
    11. +----------------------------------------+  
    12. | NULL |  
    13. +----------------------------------------+  
    14. 1 row in set, 1 warning (0.00 sec)  
    15. MySQL> select date_add(current_date, interval 1 day);  
    16. +----------------------------------------+  
    17. | date_add(current_date, interval 1 day) |  
    18. +----------------------------------------+  
    19. | 2010-01-19 |  
    20. +----------------------------------------+  
    21. 1 row in set (0.00 sec)  
    22. MySQL> select date_sub(current_timestamp, interval 1 day);  
    23. +---------------------------------------------+  
    24. | date_sub(current_timestamp, interval 1 day) |  
    25. +---------------------------------------------+  
    26. | 2010-01-17 21:28:41 |  
    27. +---------------------------------------------+  
    28. 1 row in set (0.00 sec)  
    29. MySQL> select date_sub(current_date, interval 1 day);  
    30. +----------------------------------------+  
    31. | date_sub(current_date, interval 1 day) |  
    32. +----------------------------------------+  
    33. | 2010-01-17 |  
    34. +----------------------------------------+  
    35. 1 row in set (0.00 sec)  
    36. MySQL> select date_sub(current_time, interval 1 day);  
    37. +----------------------------------------+  
    38. | date_sub(current_time, interval 1 day) |  
    39. +----------------------------------------+  
    40. | NULL |  
    41. +----------------------------------------+  
    42. 1 row in set, 1 warning (0.00 sec)  

     

    MySQL Date函数 4、时间间隔

    1. MySQL> select datediff('2010-01-18','2010-01-17');  
    2. +-------------------------------------+  
    3. | datediff('2010-01-18','2010-01-17') |  
    4. +-------------------------------------+  
    5. | 1 |  
    6. +-------------------------------------+  
    7. 1 row in set (0.00 sec)  
    8. MySQL> select timediff('2010-01-18 12:00','2010-01-17 11:00');  
    9. +-------------------------------------------------+  
    10. | timediff('2010-01-18 12:00','2010-01-17 11:00') |  
    11. +-------------------------------------------------+  
    12. | 25:00:00 |  
    13. +-------------------------------------------------+  
    14. 1 row in set (0.00 sec)  

    MySQL Date函数 5、时间转换

    1. MySQL> select time_to_sec('25:00:00');  
    2. +-------------------------+  
    3. | time_to_sec('25:00:00') |  
    4. +-------------------------+  
    5. | 90000 |  
    6. +-------------------------+  
    7. 1 row in set (0.00 sec)  
    8. MySQL> select sec_to_time(90000);  
    9. +--------------------+  
    10. | sec_to_time(90000) |  
    11. +--------------------+  
    12. | 25:00:00 |  
    13. +--------------------+  
    14. 1 row in set (0.00 sec)   

    以上的相关内容就是对MySQL Date函数的介绍,望你能有所收获。

  • 相关阅读:
    hdu 4614 线段树 二分
    cf 1066d 思维 二分
    lca 最大生成树 逆向思维 2018 徐州赛区网络预赛j
    rmq学习
    hdu 5692 dfs序 线段树
    dfs序介绍
    poj 3321 dfs序 树状数组 前向星
    cf 1060d 思维贪心
    【PAT甲级】1126 Eulerian Path (25分)
    【PAT甲级】1125 Chain the Ropes (25分)
  • 原文地址:https://www.cnblogs.com/webnote/p/5745981.html
Copyright © 2011-2022 走看看