mysql now 函数偏移_MySQL NOW() 函数

Summary: in this tutorial, you will learn about the MySQL NOW() function and apply the NOWfunction in various contexts.

Introduction to MySQL NOW() function

The MySQL NOW()function returns the current date and time in the configured time zone as a string or a number in the 'YYYY-MM-DD HH:MM:DD' or 'YYYYMMDDHHMMSS.uuuuuu'format.

The returned type of the NOW()function depends on the context where it is used. For example, in the following statement, the NOW()function returns the current date and time as a string:SELECT NOW();

AAffA0nNPuCLAAAAAElFTkSuQmCC

However, in the numeric context as the following example, the NOW()function returns the current date and time as a number:SELECT NOW() + 0;

AAffA0nNPuCLAAAAAElFTkSuQmCC

Notice that the NOW()function returns a constant date and time at which the statement started executing. See the following example:SELECT NOW(), SLEEP(5), NOW();

AAffA0nNPuCLAAAAAElFTkSuQmCC

In the query, the first NOW()function executed, and the SLEEP(5)function paused the execution of the query for 5 seconds, and the second NOW() function executed. However, both NOW() functions return the same value though they executed at the different times.

If you want to get exact time at which the statement executes, you need to use SYSDATE()instead; see the following example:SELECT SYSDATE(), SLEEP(5), SYSDATE();

AAffA0nNPuCLAAAAAElFTkSuQmCC

If you want to change the MySQL server’s time zone to adjust the current date and time returned by the NOW() function, you use the following statement:SET time_zone = your_time_zone;

MySQL NOW() function calculations

Because the NOW() function returns a number when it is used in a numeric context, you can use it in calculations e.g., now plus 1 hour, now minus 1 hour, now plus 1 day and now minus in day, etc.

The following statement returns the current date and time, now minus 1 hour and now plus 1 hour:-- mysql now minus 1 hour SELECT (NOW() - INTERVAL 1 HOUR) 'NOW - 1 hour',         NOW(), -- mysql now plus 1 hour        NOW() + INTERVAL 1 HOUR 'NOW + 1 hour';

AAffA0nNPuCLAAAAAElFTkSuQmCC

The following statement returns the current date and time, now minus 1 day and now plus 1 day:-- mysql now minus 1 day SELECT (NOW() - INTERVAL 1 DAY) 'NOW - 1 day',         NOW(), -- mysql now plus 1 day         (NOW() + INTERVAL 1 DAY) 'NOW + 1 day';

AAffA0nNPuCLAAAAAElFTkSuQmCC

MySQL NOW() as a default value for a column

You can use the NOW() function as a default value for a DATETIMEor TIMESTAMPcolumn. When you omit the date or time value  in the INSERT statement, MySQL inserts the current date and time into the column whose default value is   NOW() .

Let’s take a look at the following example.

First, create a new table named tmpwith three columns: id, titleand  created_on. The created_oncolumn has default value specified by the NOW()function.CREATE TABLE tmp(     id INT PRIMARY KEY AUTO_INCREMENT,     title VARCHAR(255) NOT NULL,     created_on DATETIME NOT NULL DEFAULT NOW() -- or CURRENT_TIMESTAMP );

Notice that CURRENT_TIMESTAMPand CURRENT_TIMESTAMP() are synonyms for NOW() so you can use them interchangeably.

Second, insert a new row into the tmptable without specifying the value for the created_oncolumn:INSERT INTO tmp(title) VALUES('Test NOW() function');

Third, query the data from the tmptable:SELECT * FROM tmp;

AAffA0nNPuCLAAAAAElFTkSuQmCC

The value of the created_oncolumn has been updated to the current date and time at which the INSERTstatement executed.

In this tutorial, we have introduced you to MySQL NOW() function that returns the current date and time at which the statement executed.

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值