• 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函数的介绍,望你能有所收获。

  • 相关阅读:
    组合模式
    迭代器模式
    模板方法模式
    外观模式
    适配器模式
    运行mysql时,提示Table ‘performance_schema.session_variables’ doesn’t exist
    idea maven 打包 引用本地jar
    centos7 安装redis
    centos7 防火墙
    linux 常用命令
  • 原文地址:https://www.cnblogs.com/webnote/p/5745981.html
Copyright © 2020-2023  润新知