eygle.com   eygle.com
eygle.com eygle
eygle.com  
 

« ORA-00600 15711 错误和 ogg goldengate 的集成故障 | Blog首页 | AWR 报告解读:Time Model Statistics 信息的计算和获取 »

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

在 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


历史上的今天...
    >> 2009-09-05文章:
    >> 2007-09-05文章:
    >> 2005-09-05文章:
           半夜三更 磁盘又坏

By eygle on 2019-09-05 13:26 | Comments (0) | FAQ | 3359 |


CopyRight © 2004~2020 云和恩墨,成就未来!, All rights reserved.
数据恢复·紧急救援·性能优化 云和恩墨 24x7 热线电话:400-600-8755 业务咨询:010-59007017-7040 or 7037 业务合作: marketing@enmotech.com