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

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

6ee5639a40442445944d63b514b2dd02.png

在 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()的一个同义词,按照同样的行为工作了。

By eygle on 2019-09-05 13:26 |

Comments (0) |

FAQ | 3359 |

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值