暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

MySQL 基础:获取当前日期的时间函数 now 和sysdate

原创 eygle 2019-09-05
979

在 MySQL 中,获得系统当前时间可以使用 now() 函数,这是最简单和应用最广的函数。

除此之外,current_timestamp(),localtime(),localtimestamp() 都是 now() 函数的同义词,返回的结果相同:

mysql> select now();

+---------------------+

| now() |

+---------------------+

| 2019-09-05 12:28:16 |

+---------------------+

1 row in set (0.00 sec)


mysql> select current_timestamp(),localtime(),localtimestamp();

+---------------------+---------------------+---------------------+

| current_timestamp() | localtime() | localtimestamp() |

+---------------------+---------------------+---------------------+

| 2019-09-05 12:30:43 | 2019-09-05 12:30:43 | 2019-09-05 12:30:43 |

+---------------------+---------------------+---------------------+

1 row in set (0.00 sec)


配套的,实现同样效果的同义词还有 current_timestamp,localtime,localtimestamp :

mysql> select current_timestamp,localtime,localtimestamp;

+---------------------+---------------------+---------------------+

| current_timestamp | localtime | localtimestamp |

+---------------------+---------------------+---------------------+

| 2019-09-05 12:30:31 | 2019-09-05 12:30:31 | 2019-09-05 12:30:31 |

+---------------------+---------------------+---------------------+

1 row in set (0.00 sec)


now() 函数在一个 SQL 执行的过程中,取得的是执行开始的时间,并且在执行过程中保持不变,与之相对的则是 sysdate() 函数,sysdate 模拟 Oracle 数据库的实现,每次执行时,都调用时间函数获得时间,数值每次不同:

mysql> select now(),sysdate(),sleep(3),now(),sysdate() ;

+---------------------+---------------------+----------+---------------------+---------------------+

| now() | sysdate() | sleep(3) | now() | sysdate() |

+---------------------+---------------------+----------+---------------------+---------------------+

| 2019-09-05 13:34:47 | 2019-09-05 13:34:47 | 0 | 2019-09-05 13:34:47 | 2019-09-05 13:34:50 |

+---------------------+---------------------+----------+---------------------+---------------------+

1 row in set (3.00 sec)

在 MySQL的源码中,可以看到这行注释,item_func_sysdate_local 模拟了 Oracle 的行为,每次执行获取当前的真实时间 - Real current time,而不是 query_start() 的时间:

00516 /*
00517 This is like NOW(), but always uses the real current time, not the
00518 query_start(). This matches the Oracle behavior.
00519 */
00520 class Item_func_sysdate_local :public Item_func_now
00521 {
00522 public:
00523 Item_func_sysdate_local() :Item_func_now() {}
00524 Item_func_sysdate_local(Item *a) :Item_func_now(a) {}
00525 bool const_item() const { return 0; }
00526 const char *func_name() const { return "sysdate"; }
00527 void store_now_in_TIME(TIME *now_time);
00528 double val_real();
00529 longlong val_int();
00530 int save_in_field(Field *to, bool no_conversions);
00531 String *val_str(String *str);
00532 void fix_length_and_dec();
00533 bool get_date(TIME *res, uint fuzzy_date);
00534 void update_used_tables()
00535 {
00536 Item_func_now::update_used_tables();
00537 used_tables_cache|= RAND_TABLE_BIT;
00538 }
00539 };

除了 sysdate() ,之外,curdate() 和 curtime() 还能够直接将日期和时间拆分开来:

mysql> select curdate(),curtime();

+------------+-----------+

| curdate() | curtime() |

+------------+-----------+

| 2019-09-05 | 13:37:14 |

+------------+-----------+

1 row in set (0.00 sec)

最后,如果你觉得now()函数就够了,可以在MySQL启动时指定 -sysdate-is-now,sysdate()就会被当成now()的一个同义词,按照同样的行为工作了。


参考:
http://mysql.localhost.net.ar/sources/doxygen/mysql-5.1/item__timefunc_8h-source.html




「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论