此文章主要向大家描述的是MySQL Date函数的实际应用以及对其实际应用代码的描述,以下就是文章的主要内容描述,望你会有所收获。

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

MySQL Date函数的正确用法(mysql中date函数)  Date函数 第1张

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

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

MySQL Date函数 2、Unix时间

  1. MySQL>selectunix_timestamp();
  2. +------------------+
  3. |unix_timestamp()|
  4. +------------------+
  5. |1263821184|
  6. +------------------+
  7. 1rowinset(0.00sec)
  8. MySQL>selectfrom_unixtime(1263821182);
  9. +---------------------------+
  10. |from_unixtime(1263821182)|
  11. +---------------------------+
  12. |2010-01-1821:26:22|
  13. +---------------------------+
  14. 1rowinset(0.00sec)

MySQL Date函数 3、时间前后

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

MySQL Date函数 4、时间间隔

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

MySQL Date函数 5、时间转换

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

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

【编辑推荐】

  1. MySQL数据库安全设置的操作流程
  2. 使用MySQL管理心得讲述
  3. MySQL基本操作,新手入门宝典
  4. MySQL 操作日志查看的实际操作步骤与代码
  5. MySQL游标的使用笔记大全
转载请说明出处
知优网 » MySQL Date函数的正确用法(mysql中date函数)

发表评论

您需要后才能发表评论