问题描述:
今天在看mysql的时间函数,now()和sysdate(),记录下两者之间有什么不同.
实验过程:
1.执行以下的两个语句:
mysql> select now(),sleep(2),now(); +---------------------+----------+---------------------+ | now() | sleep(2) | now() | +---------------------+----------+---------------------+ | 2018-06-01 15:17:18 | 0 | 2018-06-01 15:17:18 | +---------------------+----------+---------------------+ 1 row in set (2.00 sec) mysql> select sysdate(),sleep(2),sysdate(); +---------------------+----------+---------------------+ | sysdate() | sleep(2) | sysdate() | +---------------------+----------+---------------------+ | 2018-06-01 15:17:34 | 0 | 2018-06-01 15:17:36 | +---------------------+----------+---------------------+ 1 row in set (2.00 sec)
备注:通过以上的查询,看出now()在进行休眠2秒之后,再次执行还是和开始的时间是一样的,对于sysdate函数,在同一个语句中,执行了两次,第二次就是休眠2秒之后的真正的时间.
2.多次调用验证
mysql> select now(),sleep(2),now(),sleep(1),now(),now(); +---------------------+----------+---------------------+----------+---------------------+---------------------+ | now() | sleep(2) | now() | sleep(1) | now() | now() | +---------------------+----------+---------------------+----------+---------------------+---------------------+ | 2018-06-01 15:21:29 | 0 | 2018-06-01 15:21:29 | 0 | 2018-06-01 15:21:29 | 2018-06-01 15:21:29 | +---------------------+----------+---------------------+----------+---------------------+---------------------+ 1 row in set (3.00 sec) mysql> select sysdate(),sleep(2),sysdate(),sleep(1),sysdate(),sysdate(); +---------------------+----------+---------------------+----------+---------------------+---------------------+ | sysdate() | sleep(2) | sysdate() | sleep(1) | sysdate() | sysdate() | +---------------------+----------+---------------------+----------+---------------------+---------------------+ | 2018-06-01 15:21:52 | 0 | 2018-06-01 15:21:54 | 0 | 2018-06-01 15:21:55 | 2018-06-01 15:21:55 | +---------------------+----------+---------------------+----------+---------------------+---------------------+ 1 row in set (3.00 sec)
小结:
now()返回的时间是SQL语句执行的时间,无论在一次SQL语句中now()函数被执行多少次.即SQL开始执行的时间.
sysdate()返回的时间是函数执行的时间,比如以上的一条SQL语句中执行了2次,第二次就是sysdate()执行的时间.即sysdate()执行的时间.
转自:https://www.cnblogs.com/chuanzhang053/p/9121847.html